Endless adapter for ViewPager

左心房为你撑大大i 提交于 2019-12-20 10:38:45

问题


I've been using CWAC's EndlessAdapter to achieve infinite scrolling on ListViews.

I'd like to accomplish the equivalent for a ViewPager. Unfortunately, PageAdapter and ListAdapter do not share the same common base class.

What's the best way to go about this? Does a library exist that already handles this?


回答1:


What's the best way to go about this?

Add "endless" logic to your own implementation of PagerAdapter. Or, if you wish, try creating a decorating PagerAdapter, the way that EndlessAdapter decorates a regular Adapter.

The latter is likely to be tricky, given that PagerAdapter is designed for pages to be views or fragments, and the fragment handling inside of classes like FragmentPagerAdapter is a bit scary.

Does a library exist that already handles this?

None that I am aware of.

Mainly, that is because the use case doesn't seem as compelling. With a ListView, the user can fling the list, scrolling through dozens or hundreds of rows very quickly. Hence, using "we got to the end" as the trigger to load more data seems reasonable. With a ViewPager, though, it typically takes a lot longer to get to the end, particularly if you are not using PagerTabStrip or the equivalent. Hence, waiting until the user gets all the way to the end to begin loading additional data seems like it would be annoying to the user -- you had all this time to go retrieve more data, but didn't use it.

An alternative, therefore, is for you to register a ViewPager.OnPageChangeListener with your ViewPager. When onPageSelected(), and you consider yourself to be close to the end, kick off an AsyncTask (or whatever) to go gather more data. The catch then is that you will need to update the data used by the PagerAdapter and call notifyDataSetChanged() on that adapter once the data has been updated.




回答2:


@Override
public int getCount() {
    return (Integer.MAX_VALUE);
    //artificially large value for infinite scrolling
}

public int getRealCount(){
//Do something to return the actual number of objects.
}   


@Override
public Object instantiateItem(ViewGroup container, int position) {  
    int virtualPosition = position % getRealCount();
    return instantiateVirtualItem(container, virtualPosition);
}

public Object instantiateVirtualItem(ViewGroup container, final int position) {             
//Do the required part here
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    int virtualPosition = position % getRealCount();
    destroyVirtualItem(container, virtualPosition, object);
}


public void destroyVirtualItem(ViewGroup container, int position, Object object){
    container.removeView((View) object);            
}

Now, the most important part

pager.setOffscreenPageLimit(10); //your choice
pager.setCurrentItem(Integer.MAX_VALUE/2,false);
//pager is the ViewPager object

PS: I have successfully implemented this. Ask if you still have doubt.




回答3:


Maybe you can 'fake it out' as follows:

You are likely to show a huuuuge number of pages. Use FragmentStatePagerAdapter class: https://developer.android.com/reference/android/support/v13/app/FragmentStatePagerAdapter.html

Implement the getCount method by returning Integer.MAX_VALUE.
Implement the getItemPosition method by always returning POSITION_NONE.
Implement the getItem method as you wish, returning the appropriate Fragment.

Then, when the Activity that hosts the ViewPager starts, set the initial position of the ViewPager to a very large number, e.g. viewPager.setCurrentItem(Integer.MAX_VALUE / 2);.

I haven't tried this myself..., YMMV! :)



来源:https://stackoverflow.com/questions/15446806/endless-adapter-for-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!