gpt4 book ai didi

android - ViewPager.setCurrentItem 在 PagerAdapter 中返回错误的位置

转载 作者:行者123 更新时间:2023-11-29 02:30:51 25 4
gpt4 key购买 nike

这是我的 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();
}
}

I'm trying to create dynamic tab layout, but when the tab already showed up, i get wrong data because the position i use in PagerAdapter is wrong.我已经使用了 setOffscreenPageLimit 并且没有工作,我很困惑。这种情况有什么解决办法吗?非常感谢。

最佳答案

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.

关于android - ViewPager.setCurrentItem 在 PagerAdapter 中返回错误的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49686458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com