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

后端 未结 2 1037
感情败类
感情败类 2020-12-13 00:18

I am using PagerSlidingTabStrip in my project and am showing data in fragments. Its a great library which works great. Each Fragment consists of a scholar\'s le

相关标签:
2条回答
  • 2020-12-13 01:02
    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 01:08

    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;
         }
     }
    
    0 讨论(0)
提交回复
热议问题