Android的改变里面FragmentPagerAdapter片段秩序 [英] Android changing fragment order inside FragmentPagerAdapter

查看:150
本文介绍了Android的改变里面FragmentPagerAdapter片段秩序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我〜20个片段里面包含它来自何处所以世界上没有碎片娱乐采片段列表FragmentPagerAdapter。

I have ~20 fragments inside FragmentPagerAdapter that contains a list where it picks the fragments from so theres no recreation of fragments.

private List<TitledFragment> fragments;

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);

    this.fragments = new ArrayList<TitledFragment>();
}

@Override
public Fragment getItem(int i) {
    return fragments.get(i).getFragment();
}

@Override
public int getCount() {
    return fragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
   return fragments.get(position).getTitle();
}

public synchronized List<TitledFragment> getFragments() {
    return fragments;
}


public synchronized void addFragment(TitledFragment fragment) {
    fragments.add(fragment);
    notifyDataSetChanged();
}

在FragmentPagerAdapter设置为ViewPager的适配器之后,我重新排列片段(洗牌只是测试)

the FragmentPagerAdapter is set as adapter of ViewPager and after that I reorder the fragments(shuffling was just for test)

Collections.shuffle(mSectionsPagerAdapter.getFragments());
mSectionsPagerAdapter.notifyDataSetChanged();

和出于某种原因,只有标题秩序正在改变,即使其返回的的getItem不同片段的顺序不同。这是为什么,我该如何解决呢?

and for some reason only order of titles is being changed, even if its returning different fragment for getItem as the order is different. Why is this and how can I get around it?

推荐答案

您还需要重写<一个href=\"http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#getItemPosition%28java.lang.Object%29\"><$c$c>getItemPosition().默认情况下,这个函数总是返回 POSITION_UNCHANGED ,这样,当你调用 notifyDataSetChanged(),适配器假定所有的该片段是仍然在相同的位置。 (它不叫的getItem()再次,因为它认为一切都已经建立了必要的片段。)您的新 getItemPosition()应洗牌后返回每个片段的新位置。

You also need to override getItemPosition(). By default, this function always returns POSITION_UNCHANGED, so that when you call notifyDataSetChanged(), the adapter assumes that all of the fragments are still in the same position. (It does not call getItem() again, since it thinks all of the necessary fragments are already created.) Your new getItemPosition() should return the new position of each fragment after shuffling.

这是简单的黑客是总是返回 POSITION_NONE ,然后将适配器将只需放弃所有现有的片段,并在正确的位置重新创建这些调用<$ c当$ C>的getItem()。当然,这是效率不高,并且具有当前片段正在观看可能改变的附加缺点。但是,由于有可能被一些错误的重新排序的片段 - 见<一href=\"http://stackoverflow.com/questions/12510404/reorder-pages-in-fragmentstatepageradapter-using-getitempositionobject-object\">my这里的问题 - 。在 POSITION_NONE 的方法可能是最安全的方法

An easy hack is to always return POSITION_NONE, and then the adapter will just discard all of the existing fragments, and recreate them in their correct positions when calling getItem(). Of course, this is not efficient, and has the additional drawback that the current fragment being viewed may change. However, since there may possibly be some bugs in reordering the fragments - see my question here - the POSITION_NONE approach may be the safest method.

这篇关于Android的改变里面FragmentPagerAdapter片段秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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