How to load fragment data only when its tab is clicked in PagerSlidingTabStrip

喜你入骨 提交于 2019-11-28 16:51:10

You could override setUserVisibleHint event of the fragment to know if its visible to the user and then load your data. something like following:

boolean _areLecturesLoaded = false;

@Override
 public void setUserVisibleHint(boolean isVisibleToUser) {
     super.setUserVisibleHint(isVisibleToUser);
     if (isVisibleToUser && !_areLecturesLoaded ) {
      loadLectures(); 
      _areLecturesLoaded = true;
     }
 }
private boolean isLoaded =false,isVisibleToUser;

@Override

public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    this.isVisibleToUser=isVisibleToUser;
    if(isVisibleToUser && isAdded() ){
        loadData();
        isLoaded =true;
    }
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  if(isVisibleToUser && (!isLoaded)){
        loadData();
        isLoaded=true;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!