Changing Viewpager to Enable Infinite Page Scrolling

Changing ViewPager to enable infinite page scrolling

Thank you for your answer Shereef.

I solved it a little bit differently.

I changed the code of the ViewPager class of the android support library. The method setCurrentItem(int)

changes the page with animation. This method calls an internal method that requires the index and a flag enabling smooth scrolling. This flag is boolean smoothScroll.
Extending this method with a second parameter boolean smoothScroll solved it for me.
Calling this method setCurrentItem(int index, boolean smoothScroll) allowed me to make it scroll indefinitely.

Here is a full example:

Please consider that only the center page is shown.
Moreover did I store the pages seperately, allowing me to handle them with more ease.

private class Page {
View page;
List<..> data;
}
// page for predecessor, current, and successor
Page[] pages = new Page[3];




mDayPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

@Override
public void onPageScrollStateChanged(int state) {

if (state == ViewPager.SCROLL_STATE_IDLE) {

if (mFocusedPage == 0) {
// move some stuff from the
// center to the right here
moveStuff(pages[1], pages[2]);

// move stuff from the left to the center
moveStuff(pages[0], pages[1]);
// retrieve new stuff and insert it to the left page
insertStuff(pages[0]);
}
else if (mFocusedPage == 2) {


// move stuff from the center to the left page
moveStuff(pages[1], pages[0]);
// move stuff from the right to the center page
moveStuff(pages[2], pages[1]);
// retrieve stuff and insert it to the right page
insertStuff(pages[2]);
}

// go back to the center allowing to scroll indefinitely
mDayPager.setCurrentItem(1, false);
}
}
});

However, without Jon Willis Code I wouldn't have solved it myself.

EDIT: here is a blogpost about this:

Infinite Scrolling Image ViewPager

I had the same problem, but I was able to find a way to solve it - the code can be found on github.

If you copy the classes InfiniteViewPager and InfinitePagerAdapter into your project, you can achieve infinite (wrapped) scrolling with some small changes.

In your Activity, wrap your PagerAdapter with an InfinitePagerAdapter:

PagerAdapter adapter = new InfinitePagerAdapter(new ImagePagerAdapter(this, imageArra, stringArray));

Change the ViewPager in the activity XML to be an InfiniteViewPager:

<com.antonyt.infiniteviewpager.InfiniteViewPager 
android:id="@+id/myimagepager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

You can rename the classes to whatever you want. This code only works if you have at least three pages (you have nine in your example code, so it will work fine for that).

Infinite scrolling in viewpager2

For infinite scrolling, you don't have to customise the view pager. The trick in the viewPager adapter. You can return Int.MAX value in getCount override method. The in the instantiateItem override function you can use item_position % item_size to get the actual item position. Code example is given below.

class AdsSliderAdapter(
private val bannerImageList: MutableList<SliderImagesItem>,
private val callback: SliderItemClickListener) :PagerAdapter() {

private var mContext: Context? = null
override fun instantiateItem(container: ViewGroup, position: Int): Any {
mContext = container.context

val view = LayoutInflater.from(container.context)
.inflate(R.layout.item_ad_slider, container, false)

val adImage: AppCompatImageView = view.ivAd
mContext?.let {
GlideApp.with(it).load(bannerImageList[position % bannerImageList.size].imageUrl)
.into(adImage)
}
val viewPager = container as ViewPager
viewPager.addView(view, 0)

view.cardView.onClick { callback.onSliderImageClick(bannerImageList[position % bannerImageList.size]) }
return view
}

override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object`
}

override fun getCount(): Int {
return if (bannerImageList.size > 0) {
Int.MAX_VALUE
} else {
0
}
}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
val viewPager = container as ViewPager
val view = `object` as View
viewPager.removeView(view)
}

interface SliderItemClickListener {
fun onSliderImageClick(item: SliderImagesItem)
}

}

RecyclerView infinite scroll not working inside ViewPager2

I ended up fixing this by just using a different infinite scroll RecyclerView class:

https://stackoverflow.com/a/26561717/14968122

Infinite ViewPager

I was able to implement this using a little magic in my MonthViews, my PagerAdapter's OnPageChangeListener, as well as with editing the source code of ViewPager itself. If anyone is interested in how I achieved it, check out the source code or comment with a specific question.



Related Topics



Leave a reply



Submit