Lazy load data for ViewPager Items

2020-07-30 02:09发布

I have a Fragment that contains a ViewPager , now the ViewPager contains 3 different items , i.e each item(Page) contains a ListView and each list hase it Own Adapter , I cant use FragmentPagerAdapter

sine the ViewPager lives in a Fragment .

Should i Load all the Three ListViews Data in the Fragment and pass it to the PagerAdapter ?

2条回答
够拽才男人
2楼-- · 2020-07-30 02:43
   public  abstract class BaseLazyFragment extends Fragment {

    protected boolean isVisible;


    /**
     * to judge if is visible to user 
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(getUserVisibleHint()) {
            this.isVisible = true;
            onVisible();
        } else {
            this.isVisible = false;
            onInvisible();
        }

    }


    /**
     * when the  fragment is not visible to user 
     */
    protected void onInvisible() {
        Loggy.e("do  onInvisible");
    }

    /**
     * when the fragment is visible to user 
     * 
     * lazyload
     */
    protected void onVisible() {
        Loggy.e("do  onVisible---");
        lazyLoad();
    }

    protected abstract void lazyLoad();

    }

then you can create a customer fragment extends BaseLazyFragment for example

   public class NewsFragment extends BaseLazyFragment {

    private boolean isPrepared; 
    //a flag to judge init action has finished

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_news_page,null);

        //do init

        // when init is finish , set flag true;
        isPrepared = true;

        lazyLoad();

        return view;
    }

    @Override
    protected void lazyLoad() {

         // according this judge, can prevent nullpointerexception 
        if(!isPrepared || !isVisible){
            return;
        }

        // assignment the view 
        // get data from network assignment to varible
    }
}

setUserVisibleHint()

the first time when the fragment create setUserVisibleHint() method is header than onCreateView(),so the lazyLoad() method may throw NullPointerException because the oncreateView() is not invoke, so you should to create a flag variable --> isPrepared.

onCreateView()

reference link

查看更多
贼婆χ
3楼-- · 2020-07-30 02:48

Actually it depends how big is your data which are you loading?

If it is big, try to download ie. 10 items for each page and pass that data to adapters and cache it. After what, implement lazy loader (like in google play app), which downloads your next items for your correct page, not for all.

So all your three pages would have data to display on a first load, if user wants more, he can navigate to the bottom of your list, press "load more" and get more items :)

From my experience this solution works fine. Of course server side should be adopted for this kind of loader.

Also there are more considerations, ie. maybe your data is "very live". So you have to think about it.

查看更多
登录 后发表回答