getItem()位置并非总是正确!有时它会显示0,1,2,3,4,那么如果我回去显示不同的位置(4,2,3,1,0)。如果我需要位置传递到数组列表/树形图,那么使用“位置”的正确方法是什么?将位置作为参数传递给Fragment在FragmentPagerAdapter中
02-18 20:07:22.453 11479-11479/com.app E/aaa﹕ position created:0
02-18 20:07:22.453 11479-11479/com.app E/aaa﹕ position created:1
02-18 20:07:29.053 11479-11479/com.app E/aaa﹕ position created:2
02-18 20:07:35.503 11479-11479/com.app E/aaa﹕ position created:0
行:
return SingleCard.newInstance(get_all_cards_as_objects().get(position));
需要的正确位置,不会被渲染到内存中的一个!我需要片段中的数据随实际页面位置而变化。它会根据实际的页面位置知道要抓取的数据(来自地图)。
final ViewPager pager = (ViewPager) findViewById(R.id.pager);
this.parent_nested_fragment_activity = this;
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()){
private final int PARENT_COUNT = get_all_cards_as_objects().size();
@Override
public Object instantiateItem(ViewGroup container, final int position) {
Object obj = super.instantiateItem(container, position);
pager.setObjectForPosition(obj, position);
return obj;
}
@Override
public Fragment getItem(int position) {
Toast.makeText(CardDealer.this,"Selected page position: " + position, Toast.LENGTH_SHORT).show();
Log.e("aaa", "position created:" + position);
return SingleCard.newInstance(get_all_cards_as_objects().get(position));
}
我喜欢他的问题,但这个问题的答案并没有帮助: FragmentPagerAdapter getItem wrong position
我也不清楚如何实现这个答案在我的片段:getItem() calling multiple time in FragmentPagerAdapter
就像这样: Weird dissappearing Fragments with FragmentPagerAdapter
基本上我已经研究过:
pager.getCurrentItem();
如何通过getItem()将其传递到已创建的片段中?
return SingleCard.newInstance(get_all_cards_as_objects().get(pager.getCurrentItem()));