Fragment可见状态的判断

来源:互联网 发布:天猫跟淘宝有啥区别 编辑:程序博客网 时间:2024/05/02 01:08

好久好久没写博客了。。。

最近在判断Fragment可见状态时遇到个小坑,在stack over flow上找了些方法,Check fragment is currently visible or not in android?,都不是很完美。折腾了一番,总算搞出了个还算可行的办法,自己用着挺好。

At first, i defined a base Adapter help me to get the Fragment in ViewPager.If you Fragment’s position and type is changeable in ViewPager, you must use public int getItemPosition(Object object) method and public Fragment getItem(int position) method to get the right position.

/** * Created by Kilnn on 2017/7/12. * A abstract FragmentStatePagerAdapter which hold the fragment reference. */public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {    private SparseArray<WeakReference<Fragment>> registeredFragments = new SparseArray<>();    public SmartFragmentStatePagerAdapter(FragmentManager fm) {        super(fm);    }    @Override    public Object instantiateItem(ViewGroup container, int position) {        Fragment fragment = (Fragment) super.instantiateItem(container, position);        registeredFragments.put(position, new WeakReference<>(fragment));        return fragment;    }    @Override    public void destroyItem(ViewGroup container, int position, Object object) {        registeredFragments.remove(position);        super.destroyItem(container, position, object);    }    public Fragment getRegisteredFragment(int position) {        WeakReference<Fragment> reference = registeredFragments.get(position);        return reference != null ? reference.get() : null;    }}

At then ,i define a base Fragment to handle the visible state.But there is a little flaw is that you must specify same switch type for one set of fragment .

import android.support.annotation.IntDef;import android.support.v4.app.Fragment;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;/** * Created by Kilnn on 2017/7/12. * A smart fragment know itself's visible state. */public abstract class SmartFragment extends Fragment {    private boolean isFragmentVisible;    @Override    public void onResume() {        super.onResume();        int switchType = getSwitchType();        if (switchType == ATTACH_DETACH) {            notifyOnFragmentVisible();        } else if (switchType == SHOW_HIDE) {            if (!isHidden()) {                notifyOnFragmentVisible();            }        } else if (switchType == VIEW_PAGER) {            //If the parent fragment exist and hidden when activity destroy,            //when the activity restore, The parent Fragment  will be restore to hidden state.            //And the sub Fragment which in ViewPager is also be restored, and the onResumed() method will callback.            //And The sub Fragment's getUserVisibleHint() method will return true  if it is in active position.            //So we need to judge the parent Fragment visible state.            if (getUserVisibleHint() && isParentFragmentVisible()) {                notifyOnFragmentVisible();            }        }    }    @Override    public void setUserVisibleHint(boolean isVisibleToUser) {        super.setUserVisibleHint(isVisibleToUser);        int switchType = getSwitchType();        if (switchType == VIEW_PAGER) {            if (isVisibleToUser) {                //If ViewPager in ViewPager , the sub ViewPager will call setUserVisibleHint before onResume                if (isResumed()) {                    notifyOnFragmentVisible();                }            } else {                notifyOnFragmentInvisible();            }        }    }    @Override    public void onHiddenChanged(boolean hidden) {        super.onHiddenChanged(hidden);        int switchType = getSwitchType();        if (switchType == SHOW_HIDE) {            if (hidden) {                notifyOnFragmentInvisible();            } else {                //Just judge for safe                if (isResumed()) {                    notifyOnFragmentVisible();                }            }        }    }    @Override    public void onPause() {        super.onPause();        notifyOnFragmentInvisible();    }    private boolean isParentFragmentVisible() {        Fragment parent = getParentFragment();        if (parent == null) return true;        if (parent instanceof SmartFragment) {            return ((SmartFragment) parent).isFragmentVisible();        } else {            //TODO May be can't get the correct visible state if parent Fragment is not SmartFragment            return parent.isVisible();        }    }    public boolean isFragmentVisible() {        // Don't judge the state of the parent fragment,        // because if the parent fragment visible state changes,        // you must take the initiative to change the state of the sub fragment//        return isFragmentVisible && isParentFragmentVisible();        return isFragmentVisible;    }    public void notifyOnFragmentVisible() {        if (!isFragmentVisible) {            onFragmentVisible();            isFragmentVisible = true;        }    }    public void notifyOnFragmentInvisible() {        if (isFragmentVisible) {            onFragmentInvisible();            isFragmentVisible = false;        }    }    /**     * If this method callback, the Fragment must be resumed.     */    public void onFragmentVisible() {    }    /**     * If this method callback, the Fragment maybe is resumed or in onPause().     */    public void onFragmentInvisible() {    }    /**     * Fragments switch with attach/detach(replace)     */    public static final int ATTACH_DETACH = 0;    /**     * Fragments switch with show/hide     */    public static final int SHOW_HIDE = 1;    /**     * Fragments manage by view pager     */    public static final int VIEW_PAGER = 2;    @Retention(RetentionPolicy.SOURCE)    @IntDef({ATTACH_DETACH, SHOW_HIDE, VIEW_PAGER})    public @interface SwitchType {    }    @SwitchType    public abstract int getSwitchType();}

At last, hand the sub Fragment visible state in the parent Fragment if nested use.

public class ParentFragment extends SmartFragment{    ....    @Override    public void onFragmentVisible() {        super.onFragmentVisible();        SmartFragment fragment = (SmartFragment) mAdapter.getRegisteredFragment(mViewPager.getCurrentItem());        if (fragment != null) {            fragment.notifyOnFragmentVisible();        }    }    @Override    public void onFragmentInvisible() {        super.onFragmentInvisible();        SmartFragment fragment = (SmartFragment) mAdapter.getRegisteredFragment(mViewPager.getCurrentItem());        if (fragment != null) {            fragment.notifyOnFragmentInvisible();        }    }    ...}

I have test with attach/detach, show/hide ,and three layers ViewPager nested. It work fine. Maybe I should have done more testing, but for me it was enough.

原创粉丝点击