Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段

Android ViewPager: Update off-screen but cached fragments in ViewPager(Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段)

本文介绍了Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewPager,它在其片段内包含多个 TextView,它们具有不同的字体大小.此外,我得到了增加/减少字体大小的按钮,它通过添加其默认大小加上一个名为 STEP 的值(由 inc/dec 字体大小按钮更改)来计算每个 textView 的字体大小.
我的问题是,如果在应用大小更改后第一次显示视图,它将在 onCreateView() 期间创建所需大小的 textViews 但是如果片段已被缓存并且未再次调用 onCreateView,我不知道如何更新他们的 textViews考虑到只有一个缓存片段显示在屏幕上(我可以毫无问题地更新它),但其他片段没有显示(在这种情况下,我不知道它们是否附加到活动).
换句话说,我如何检测 ViewPager 缓存了哪些片段以及它们的 onCreateView 已经调用(这些片段必须应用更新到它们的视图).我在下图中用问号将它们标记为浅绿色:

I have a ViewPager which contains several TextViews inside its fragment which they have different font sizes. In addition, I got buttons for increase/decreasing font size which calculate font size of each textView by adding its default size plus a value called STEP (which changes by inc/dec font size button).
My problem is if a view is displayed for first time after applying size changes, It will create textViews in desired size during onCreateView() however if fragment was cached already and onCreateView is not called again, I don't know how to update their textViews considering only one of cached fragments is displaying on screen (and i can update it with no problem) but others are not displayed (I don't know if they are attached to activity or not in this case).
In other words how can i detect which fragments are cached by ViewPager and their onCreateView already called (these are fragments which update to their views must be applied). I marked them in light green with question marks in below picture:

推荐答案

为了通过 ViewPager 获得缓存项目列表,我更改了我的自定义适配器,其中扩展了 FragmentStatePagerAdapter:

In order to have a list of cached items by ViewPager I changed my Custom Adapter which an extension FragmentStatePagerAdapter:

  1. 添加一个HashMap;cachedFragmentHashMap 到我的适配器
  2. 像这样更新 getItem() 方法

  1. Add a HashMap<Integer, FragmentDipsplayPageContent> cachedFragmentHashMap to my adapter
  2. Update getItem() method like this

public Fragment getItem(int index) {        
    Fragment fragment = new FragmentDipsplayPageContent();
    Bundle args = new Bundle();
    args.putInt("INDEX", index); 
    fragment.setArguments(args);
    cachedFragmentHashMap.put(index,fragment); 
    return fragment;
}

  • 像这样更新适配器的destroyItem()方法

  • Update destroyItem() method of adapter like this

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
        //add this line to remove fragments wiped from ViewPager cache
        cachedFragmentHashMap.remove(position);
    }
    

  • 添加一个 getter 以从活动中访问缓存片段的 HashMap:

  • Add a getter to access HashMap of cached Fragments from activity:

    public HashMap<Integer, FragmentDipsplayPageContent> getCachedFragmentHashMap() {
        return cachedFragmentHashMap;
    }
    

  • 在活动中使用此集合更新:

  • update using this collection inside activity:

    private void increaseFontSizeForCachedFragments() {
        HashMap<Integer, FragmentDipsplayPageContent> cachedFragmentsHashMap = adapterViewPager
                .getCachedFragmentHashMap();
        Collection<FragmentDipsplayPageContent> fragmentsCollection = cachedFragmentsHashMap
                .values();
        for (FragmentDipsplayPageContent fragmentDipsplayPageContent : fragmentsCollection) {
            //update views of fragment
            fragmentDipsplayPageContent.increasTextFontSize();
        }
    }
    


    这样所有缓存的片段(包括可见片段和屏幕外片段)都会更新.


    This way all cached fragments including visible and off-screen fragments are updated.

    这篇关于Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

  • 本文标题为:Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段

    基础教程推荐