0

アプリで (Google ソースから)を使用して、SlidingTabLayout異なるフラグメント間を移動しています。コンテンツ間をうまくスワイプできますが、私が直面している問題は、奇妙な動作をしているアクション バーのタイトルです。

2 つのタイトル (「First Title」と「Second Title」) を持つ 2 つのタブがあるとします。

| Action bar       |

| First Title | Second Title |

を含むフラグメントを最初に入力するSlidingTabLayoutと、アクションバーのタイトルは次のようになります。

| Firs...      |

| First Title | Second Title |

スワイプすると (たとえば 2 番目のタブに)、アクションバーのタイトルは次のようになります。

| Second Title       |

| First Title | Second Title |

そしてこのまま。少なくとも 1 回スワイプすると、最後に読み込まれた Fragment のタイトルがActionbar に表示されるようです。

私が欲しいのはこれです:

タイトルが何であっても変わらない「メインタイトル」をアクションバーに表示したいと思いSlidingTabLayoutます。

ここに私のコードのいくつかの部分があります:

** SlidingTabLayout を含むフラグメント:

private String mainTitle;
....

@Override
public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);

    // The mainTitle is given to this fragment by another fragment
    Bundle args = getArguments();
    if (args != null) {
        mainTitle = args.getString("TITLE");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.layout, container, false);
    mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
    mSlidingLayout = (SlidingTabLayout) rootView.findViewById(R.id.sliding_layout);
    List<Fragment> listFragments = new ArrayList<>();
    List<String> listTitles = new ArrayList<>();
    for (int i = 0; i < mSize; i++) {
        Bundle bundle = new Bundle();
        ....
        listFragments.add(Fragment.instanciate(getActivity(), CustomFragment.class.getName(), bundle));
        listTitles.add("...");
    }
    mViewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), listFragments, listTitles));

    mSlidingLayout.setDistributedEvenly(true);
    mSlidingLayout.setViewPager(mViewPager);

    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    // I TRY TO SET THE TITLE HERE !!
    getActivity().getSupportActionBar().setTitle(mainTitle);
}

** アダプター:

class PagerAdapter extends FragmentPagerAdapter {
    List<Fragment> fragments;
    List<String> titles;
    ...
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = fragments.get(position);
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // maybe the problem is in this method.
        // this method is supposed to provide the titles for the tab
        // but maybe it's also changing the actionbar title
        return titles.get(position);
}

onResumeFragment を入力するたびに、メソッドで ActionBar のタイトルを設定しています。

ありがとう !

編集1:

SlidingTabLayoutGoogle I/O から入手した新しいものを使用してみましたが、結果は同じです。

ActionBar のタイトルは最初はヒントのようですが、別のフラグメントにスワイプすると、最後に読み込まれたフラグメントに変わります。

フラグメントをロードしているようで、フラグメントがロードされるたびに、ActionBar のタイトルがそのフラグメントのタイトルで上書きされます。

編集2:

コードを変更して最新バージョンを投稿し (現在は SlidingTabLayout を使用しています)、mainTitle を取得する方法を示しています。

4

4 に答える 4

1

onResume()スワイプしたときに呼び出されないため、actionBar のタイトルは変わりません。

onResumeこれは、ViewPager がフラグメントを 2 回目に呼び出さないためです。実際にはフラグメントが再開されますが。

できることは、親フラグメント (pagertabstrip を含む) のアクションバー タイトルの設定をonCreateView代わりに移動することです。

ご参考までに:

** PagerTabStrip を含むフラグメント:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.layout, container, false);
    mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);

    List<Fragment> listFragments = new ArrayList<>();
    List<String> listTitles = new ArrayList<>();

    ...

    // HERE's what you want
    final ActionBar actionBar = getActivity().getSupportActionBar();
    if (actionBar != null) {
        actionBar.setTitle(R.string.main_title);
    }

    mViewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), listFragments));

    return rootView;
}
于 2015-09-23T14:19:52.517 に答える
1

以下のコードを使用できます

public class MatchesActivity extends AppCompatActivity implements ActionBar.TabListener {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
FloatingActionButton skipButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_matches);
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    skipButton = (FloatingActionButton) findViewById(R.id.skip_next);
    skipButton.setVisibility(View.GONE);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(1);
    mViewPager.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return false;
        }
    });
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    actionBar.addTab(
            actionBar.newTab().setText("MATCHES")
                    .setTabListener(this));
    actionBar.addTab(
            actionBar.newTab().setText("PINS")
                    .setTabListener(this));
    actionBar.addTab(
            actionBar.newTab().setText("CHATS")
                    .setTabListener(this));
}

@Override
public void onBackPressed() {
    startActivity(new Intent(MatchesActivity.this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK & Intent.FLAG_ACTIVITY_CLEAR_TOP));
    MatchesActivity.this.finish();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new MatchesFragment();
            case 1:
                return new PinsFragment();
            case 2:
                return new ConversationFragment();
            default:
                return new MatchesFragment();
        }
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

}

于 2015-09-22T11:38:09.470 に答える
1

ここに私のコードを投稿しています。うまくいきました。必要な変更を加えて試してみてください。PagerSlidingTabStrip ライブラリを使用しました。

public class DealFragment extends Fragment {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_deal, container, false);
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    viewPager.setAdapter(new SampleFragmentPagerAdapter(getChildFragmentManager()));
    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip)view.findViewById(R.id.tabs);
    tabsStrip.setBackgroundColor(Color.parseColor("#333333"));
    // Attach the view pager to the tab strip
    tabsStrip.setViewPager(viewPager);
    return view;
}
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 2;
    private final String tabTitles[] = new String[] { "Today's Deals", "Deals Close By" };

    public SampleFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        if(position==0)
        {
            return new TodaysDeal();
        }
        else
        {
            return new DealsCloseBy();
        }

    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

}

それがあなたを助けることを願っています。

于 2015-09-22T11:38:30.710 に答える
0

それを見つけた !

私は本当に馬鹿でした(他の人がこの問題を抱えていなかった理由を説明しています)

各フラグメント (タブに含まれているものも含む) にもタイトルを設定していたので、スワイプすると、それらのフラグメントの onResume が呼び出され、ActionBar のタイトルが変更されました...

助けてくれてありがとう、ありがとう!

于 2015-09-23T15:49:01.003 に答える