Tablayout+Viewpager+Frgment does the Viewapger cache, and the first time you click on the tab to refresh, but after the second t

Tablayout+Viewpager+Frgment to do the Viewapger cache, and the first time you click on the tab to refresh, but after the second time there is a cache and will not automatically refresh to actively refresh.

Need to implement this First you need to understand a method of fragment

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

}

fragment This method is not in the life cycle of the fragment, it will be called automatically when you make this fragment display true.

meaning that when we click the tab button, the fragment will automatically call this method, then we can directly do the fragment refresh in this method? The answer is no.

why? Because this method will be called before onCreateVIew, it means that when you get the network data, you will get an error when updating Ui, because your

SwipeRefreshLayout或者RecycelrView没有调用findById,没有绑定控件,那么我们就需要在第一个framgne单独提出来,其他之后的所有fragmnet都用一个fragment,因为只有第一个framgent才会出现先执行setUserVisibleHint,后执行OnCreateView方法

needs to implement the following important conditions:

1 , viewPager.setOffscreenPageLimit(list_fragment.size()-1);//cache all fragments

2、

/**
 * 当这个fragment显示的时候就去刷新,就是当你点击tab的时候并且是第一次点击就去刷新
 *
 * @param isVisibleToUser
 */
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    Log.e("isVisibleToUser", isVisibleToUser + "");
    if (isVisibleToUser && list == null) {//这个页面是显示的,并且list==null表示第一次创建的时候才去刷新,第二次打开这个//fragment  list就不=null了
        swipeRefresh.setRefreshing(true);
        onRefresh();
    }
}