CardSlider
CardSlider copied to clipboard
How to add infinite scroll in both direction?
This is my question in Stackoverflow https://stackoverflow.com/questions/60245815/viewpager2-how-to-implement-infinite-scroll-in-both-direction
I will trying to add this feature in the nearest time.
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])
}
I forgot to write this I set current position in the middle binding.vpCategories.currentItem = Integer.MAX_VALUE / 2
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?
full source code is here https://github.com/IslamKhSh/CardSlider/issues/26
full source code is here #26
thank you.
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
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
}
should i add this line in activity "slider.setCurrentItem(Integer.MAX_VALUE / 2, false);" before setting adapter or after setting adapter;
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.