Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add infinite scroll in both direction? #17

Open
TikTak123 opened this issue Feb 15, 2020 · 11 comments
Open

How to add infinite scroll in both direction? #17

TikTak123 opened this issue Feb 15, 2020 · 11 comments
Labels
enhancement New feature or request

Comments

@TikTak123
Copy link

No description provided.

@TikTak123 TikTak123 changed the title How to add infinite scroll? How to add infinite scroll in both direction? Feb 16, 2020
@TikTak123
Copy link
Author

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

@IslamKhSh
Copy link
Owner

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

@IslamKhSh IslamKhSh added the enhancement New feature or request label Feb 27, 2020
@TikTak123
Copy link
Author

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
Copy link
Author

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

@aminshabani94
Copy link

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?

@TikTak123
Copy link
Author

full source code is here #26

@aminshabani94
Copy link

full source code is here #26

thank you.

@fahad-mukhtar
Copy link

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

@aminshabani94
Copy link

aminshabani94 commented Jun 4, 2020

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
}

@fahad-mukhtar
Copy link

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

@aminshabani94
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants