ViewPager中的片段-错误的索引? [英] Fragments in ViewPager - wrong index?

查看:100
本文介绍了ViewPager中的片段-错误的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ViewPager来显示3个相同类别的ListFragments,但是每个FragmentListView内容应根据Fragment的索引而有所不同.

I'm using a ViewPager to display 3 ListFragments of the same class, but the ListView content of each Fragment should be different depending on Fragment's index.

ListFragment:

    public static NavigationListFragment newInstance(int index) {
    NavigationListFragment listFrag = new NavigationListFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("num", index);
    listFrag.setArguments(bundle);
    Log.d("INDEX", index+"");
    return listFrag;
}

FragmentPagerAdapter:

    @Override
public Fragment getItem(int index) {
    return NavigationListFragment.newInstance(index);
}

    @Override
public int getCount() {
    return TITLES.length;
}

@Override
public CharSequence getPageTitle(int position) {
    return TITLES[position];
}

问题:开始时,我立即获得INDEX 0INDEX 1的记录,当我滑动到Pager内的第二个项目时,它显示INDEX 2不显示任何内容最后一个.标题(我使用的是TabStrip)却为每个页面正确设置了.

The problem: at the start I immediately get INDEX 0 and INDEX 1 logged, when I swipe to the 2nd item inside the Pager it shows INDEX 2 and doesn't show anything for the last one. The title (I'm using a TabStrip) however is set correctly for each page.

为什么getItem()方法的第一页被调用了两次,而最后一页却根本没有被调用?

Why is the getItem() method called 2 times for the first page, but isn't called at all for the last one?

更新 我发现我不能依赖getItem()方法,因为它被调用来预加载多个Fragments.所以现在的问题是,在设置片段ListView之前,我如何安全地确定片段的位置?

UPDATE I figured out I can't rely on getItem() method since it gets called to preload multiple Fragments. So the question is now how can I safely determine the position of a Fragment before setting up its ListView?

更新2 我最终创建了3个Fragment类,并根据索引实例化了它们,但是不知何故,我觉得这是一个丑陋的解决方案,我要感谢一个更优雅的解决方案.

UPDATE 2 I ended up creating 3 Fragment classes and instantiating them based on index, but somehow I feel this is an ugly solution, I'd be thankful for a more elegant one.

推荐答案

ViewPager创建最近的项目以用于滑动动画.因此,第一次在活动附加ViewPager时,用户应该看到第一个片段,并且可以滑动到第二个片段.当用户滑动ViewPager时,将创建第三个,然后用户可以再次滑动.

ViewPager creates nearest items for swipe animation purpose. So first time when activity attaches ViewPager user should see first fragment and can swipe to second one. When user swiped ViewPager creates third one and user can swipe again.

  1. ViewPager
  2. 的附件
  3. ViewPager创建第一项和第二项-用户看到第一个片段
  4. 滑动开始-用户看到第一和第二之间的过渡
  5. 滑动结束-用户看到第二个片段
  6. ViewPager创建第三个项目
  7. 滑动开始-用户看到第二和第三之间的过渡
  8. 滑动结束-用户看到第三个片段
  9. ViewPager销毁第一个片段并创建第四个片段
  1. attachment of ViewPager
  2. ViewPager creates first and second items - user see first fragment
  3. swipe begin - user see transition between first and second
  4. swipe end - user see second fragment
  5. ViewPager creates third item
  6. swipe begin - user see transition between second and third
  7. swipe end - user see third fragment
  8. ViewPager destroys first fragment and create fourth
  9. etc

这篇关于ViewPager中的片段-错误的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆