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

查看:460
本文介绍了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<Integer, FragmentDipsplayPageContent> 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);
    }
    

  • 添加吸气剂以访问来自活动的缓存片段的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中缓存的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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