0
我有一個小問題刷新tableLayout使用ViewPager(帶片段)
現狀
我有一個ViewPager 2個片段內。
viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
//////////////////////
private void setupViewPager(ViewPager viewPager) {
adapter = new PagerAdapter (getSupportFragmentManager());
adapter.addFragment(new Frag1(), "Frag1");
adapter.addFragment(new Frag2(), "Frag2");
viewPager.setAdapter(adapter);
}
,我把它放在我TabLayout
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
它工作得很好,但現在我想在我的導航抽屜中按下按鈕時更改我的選項卡內的碎片。
這裏:
private void setupDrawerContent(final NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.nav_menu1:
break;
case R.id.nav_menu2:
here for example
break;
case R.id.nav_menu3:
break;
}
return true;
}
});
}
我應該怎麼辦呢? PS:我試着添加一些notifyDataSetChanged()
的適配器.. invalidate()
tabLayout ...我刪除了代碼,因爲我做錯了。
這裏是我的PagerAdapter
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public PagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}