这是我的tabLayoutCode
mTabLayoutBerita.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.i("onTabSelected", String.valueOf(tab.getPosition()));
mViewPagerBerita.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
这是我的PagerAdapter
public class PagerBerita extends FragmentPagerAdapter {
private int mNumOfTabs;
private FragmentBeritaByKategori fragmentBeritaByKategori = null;
private List<KategoriItem> mDataListKategoriBerita;
public PagerBerita(FragmentManager fm, int NumOfTabs, List<KategoriItem> mDataListKategoriBerita) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.mDataListKategoriBerita = mDataListKategoriBerita;
}
@Override
public Fragment getItem(int position) {
fragmentBeritaByKategori = new FragmentBeritaByKategori(mDataListKategoriBerita.get(position).getId());
return fragmentBeritaByKategori;
}
@Override
public int getCount() {
return mDataListKategoriBerita.size();
}
}
我正在尝试创建动态标签布局,但是当标签已经显示时,我得到错误的数据,因为我在PagerAdapter中使用的位置是错误的。我已经使用了setOffscreenPageLimit并且没有工作,我很困惑。这种情况的解决方案?非常感谢。
答案 0 :(得分:0)
You are forgot to add ViewPager to tab layout.
TabLayout tabsLayout = (TabLayout) findViewById(R.id.tabsLayout);
ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
//Add ViewPager to tab layout.
tabsLayout.setupWithViewPager(viewpager);
viewpager.setOffscreenPageLimit(8);
tabsLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewpager.setCurrentItem(tab.getPosition());
Log.i("TabLayout Position: " + tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
It working fine.
答案 1 :(得分:0)
我喜欢这个,是不是错了?
final CustomViewPager viewPager = findViewById(R.id.pager);
final PagerAdapterBottomNavigation pagerAdapterBottomNavigation = new PagerAdapterBottomNavigation(getSupportFragmentManager(), tab_layout.getTabCount());
viewPager.setAdapter(pagerAdapterBottomNavigation);
viewPager.setCurrentItem(2);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab_layout.getTabAt(2).getIcon().setColorFilter(getResources().getColor(R.color.secondaryTextColor), PorterDuff.Mode.SRC_IN);
tab.getIcon().setColorFilter(getResources().getColor(R.color.secondaryLightColor), PorterDuff.Mode.SRC_IN);
if(tab.getPosition() == 0) {
actionBar.setTitle("Menu");
} else if(tab.getPosition() == 1) {
actionBar.setTitle("News");
} else if(tab.getPosition() == 2) {
actionBar.setTitle("Event");
} else if(tab.getPosition() == 3) {
actionBar.setTitle("Promo");
} else if(tab.getPosition() == 4) {
actionBar.setTitle("Setting");
}
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(getResources().getColor(R.color.secondaryTextColor), PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
我的PagerAdapter
public class PagerAdapterBottomNavigation extends FragmentPagerAdapter {
int mNumOfTabs;
private FragmentAllMenu fragmentAllMenu;
private FragmentBerita fragmentBerita;
private FragmentEvent fragmentEvent;
private FragmentSetting fragmentSetting;
private FragmentPromo fragmentPromo;
public PagerAdapterBottomNavigation(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fragmentAllMenu = new FragmentAllMenu();
return fragmentAllMenu;
case 1:
fragmentBerita = new FragmentBerita();
return fragmentBerita;
case 2:
fragmentEvent = new FragmentEvent();
return fragmentEvent;
case 3:
fragmentPromo = new FragmentPromo();
return fragmentPromo;
case 4:
fragmentSetting = new FragmentSetting();
return fragmentSetting;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
但我检查了PagerAdapter getItem(int position),位置错误但显示正确的片段。