Android FragmentStatePagerAdapter,如何标记片段以便以后找到它

Android FragmentStatePagerAdapter, how to tag a fragment to find it later(Android FragmentStatePagerAdapter,如何标记片段以便以后找到它)
本文介绍了Android FragmentStatePagerAdapter,如何标记片段以便以后找到它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 FragmentStatePageAdapter 我得到这样的片段:

When using the FragmentStatePageAdapter I get the fragments like this:

    @Override
    public Fragment getItem(int position) {
        return new SuperCoolFragment();
    }

但是,稍后在我的代码中,我需要找到这个片段来设置属性.在我使用片段的应用程序的其他一些部分中,我基本上标记它们并使用 findFragmentByTag(TAG) 查找它们,但现在我不知道该怎么做.

However, later on my code I need to find this fragment to set a property. In some other parts of the app where I use fragments I basically tag them and look for them using findFragmentByTag(TAG) but with now I don't know how to do it.

如何使用 FragmentStatePageAdapter 找到片段?

How do can I find the fragments using the FragmentStatePageAdapter?

推荐答案

* EDIT *

正如@ElliotM 指出的,有一个更好的解决方案.无需更改适配器中的任何内容,只需获取片段:

As @ElliotM pointed out, there is a better solution. No need to change anything in your adapter, just get the fragment with:

Fragment myFragment = (Fragment) adapter.instantiateItem(viewPager, viewPager.getCurrentItem());

* 旧解决方案 *

最佳选择是这里的第二种解决方案:http://tamsler.blogspot.nl/2011/11/android-viewpager-and-fragments-part-ii.html

Best option is the second solution here: http://tamsler.blogspot.nl/2011/11/android-viewpager-and-fragments-part-ii.html

简而言之:您跟踪所有活动"片段页面.在这种情况下,您在 ViewPager 使用的 FragmentStatePagerAdapter 中跟踪片段页面.

In short: you keep track of all the "active" fragment pages. In this case, you keep track of the fragment pages in the FragmentStatePagerAdapter, which is used by the ViewPager..

public Fragment getItem(int index) {
    Fragment myFragment = MyFragment.newInstance();
    mPageReferenceMap.put(index, myFragment);
    return myFragment;
}

为了避免保留对非活动"片段页面的引用,我们需要实现 FragmentStatePagerAdapter 的 destroyItem(...) 方法:

To avoid keeping a reference to "inactive" fragment pages, we need to implement the FragmentStatePagerAdapter's destroyItem(...) method:

public void destroyItem(View container, int position, Object object) {
    super.destroyItem(container, position, object);
    mPageReferenceMap.remove(position);
}

...当您需要访问当前可见的页面时,您可以调用:

... and when you need to access the currently visible page, you then call:

int index = mViewPager.getCurrentItem();
MyAdapter adapter = ((MyAdapter)mViewPager.getAdapter());
MyFragment fragment = adapter.getFragment(index);

... MyAdapter 的 getFragment(int) 方法如下所示:

... where the MyAdapter's getFragment(int) method looks like this:

public MyFragment getFragment(int key) {
    return mPageReferenceMap.get(key);
}

---

在你的适配器中添加这个,在方向改变之后:

Also add this in your adapter, for after an orientation change:

/**
 * After an orientation change, the fragments are saved in the adapter, and
 * I don't want to double save them: I will retrieve them and put them in my
 * list again here.
 */
@Override
public Object instantiateItem(ViewGroup container, int position) {
    MyFragment fragment = (MyFragment) super.instantiateItem(container,
            position);
    mPageReferenceMap.put(position, fragment);
    return fragment;
}

这篇关于Android FragmentStatePagerAdapter,如何标记片段以便以后找到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)