【发布时间】:2015-04-18 20:39:39
【问题描述】:
getItem() 的位置并不总是正确的!有时它会显示 0,1,2,3,4 然后如果我返回它会显示不同的位置 (4,2,3,1,0)。如果我需要将位置传递到数组列表/树形图中,使用“位置”的正确方法是什么???
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()));
【问题讨论】:
标签: java android fragment fragmentpageradapter