getSupportFragmentManager().getFragments() deprecated?

前端 未结 1 1576
太阳男子
太阳男子 2021-01-20 23:48

I am working with a viewpager and compiling API 23. I\'m showing a compile error in my code for the following statement, but the project does compile.

List<         


        
相关标签:
1条回答
  • 2021-01-21 00:48

    I actually did this to get a reference to all the fragments:

    private List<WeakReference<Fragment>> mFragList = new ArrayList<WeakReference<Fragment>>();
    
    @Override
    public void onAttachFragment (Fragment fragment) {
        mFragList.add(new WeakReference(fragment));
    }
    
    public List<Fragment> getActiveFragments() {
        ArrayList<Fragment> ret = new ArrayList<Fragment>();
        for(WeakReference<Fragment> ref : mFragList) {
            Fragment f = ref.get();
            if(f != null) {
                if(f.isVisible()) {
                    ret.add(f);
                }
            }
        }
        return ret;
    }
    
    public Fragment findFragement(String tClass) {
    
        List<Fragment> fragments = getActiveFragments();
        for (Fragment fragment : fragments) {
            if (tClass.equalsIgnoreCase("Home")) {
                if (fragment instanceof ToggleFragment) {
                    return fragment;
    
                }
            } else if (tClass.equalsIgnoreCase("Contacts")) {
                if (fragment instanceof ContactFragment) {
                    return fragment;
                }
            }
        }
        return null;
    }
    
    0 讨论(0)
提交回复
热议问题