CardSlider icon indicating copy to clipboard operation
CardSlider copied to clipboard

How to add infinite scroll in both direction?

Open TikTak123 opened this issue 4 years ago • 11 comments

TikTak123 avatar Feb 15 '20 18:02 TikTak123

This is my question in Stackoverflow https://stackoverflow.com/questions/60245815/viewpager2-how-to-implement-infinite-scroll-in-both-direction

TikTak123 avatar Feb 16 '20 06:02 TikTak123

I will trying to add this feature in the nearest time.

IslamKhSh avatar Feb 27 '20 19:02 IslamKhSh

I implemented this feature like this.

    val items = ArrayList<Level>()

    override fun getItemCount(): Int = if (items.isEmpty()) 0 else Integer.MAX_VALUE

    override fun bindVH(holder: CategoryViewHolder, position: Int) {
        holder.bind(items[position % items.size])
    }

TikTak123 avatar Feb 29 '20 07:02 TikTak123

I forgot to write this I set current position in the middle binding.vpCategories.currentItem = Integer.MAX_VALUE / 2

TikTak123 avatar Mar 03 '20 16:03 TikTak123

I forgot to write this I set current position in the middle binding.vpCategories.currentItem = Integer.MAX_VALUE / 2

hello, I have the same problem, where should I put this in my code?

aminshabani94 avatar May 27 '20 10:05 aminshabani94

full source code is here https://github.com/IslamKhSh/CardSlider/issues/26

TikTak123 avatar May 27 '20 10:05 TikTak123

full source code is here #26

thank you.

aminshabani94 avatar May 27 '20 10:05 aminshabani94

hi, can you please tell me how to implement this thing in java as I am not able to get infinite scroll on both sides. Thanks

fahad-mukhtar avatar Jun 04 '20 07:06 fahad-mukhtar

hi, can you please tell me how to implement this thing in java as I am not able to get infinite scroll on both sides. Thanks

I did this and worked for me, first put this code in your adapter:

@Override
public int getItemCount() {
    return sliders.isEmpty() ? 0 : Integer.MAX_VALUE;// sliders is the list of my items
}

then, set the slider item at the middle by doing this in Fragment or Activity:

slider.setCurrentItem(Integer.MAX_VALUE / 2, false); // slider is CardSliderViewPager object

you also should use (position % sliders.size()) instead of position:(sliders is the list of items in my adapter)

@Override
public void bindVH(@NotNull SliderAdapter.SliderViewHolder holder, int position) {
    Utility.loadWithGlide(activity, sliders.get(position % sliders.size()).imageUrl, holder.sliderImage, null); // use (position % sliders.size()) instead of position
}

aminshabani94 avatar Jun 04 '20 17:06 aminshabani94

should i add this line in activity "slider.setCurrentItem(Integer.MAX_VALUE / 2, false);" before setting adapter or after setting adapter;

fahad-mukhtar avatar Jun 06 '20 08:06 fahad-mukhtar

should i add this line in activity "slider.setCurrentItem(Integer.MAX_VALUE / 2, false);" before setting adapter or after setting adapter;

you should add this after setting adapter.

aminshabani94 avatar Jul 23 '20 06:07 aminshabani94