Swipe one item at a time Recyclerview

28,385

Solution 1

This softens the movement between items:

public class SnapHelperOneByOne extends LinearSnapHelper {

    @Override
    public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {

        if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
            return RecyclerView.NO_POSITION;
        }

        final View currentView = findSnapView(layoutManager);

        if (currentView == null) {
            return RecyclerView.NO_POSITION;
        }

        LinearLayoutManager myLayoutManager = (LinearLayoutManager) layoutManager;

        int position1 = myLayoutManager.findFirstVisibleItemPosition();
        int position2 = myLayoutManager.findLastVisibleItemPosition();

        int currentPosition = layoutManager.getPosition(currentView);

        if (velocityX > 400) {
            currentPosition = position2;
        } else if (velocityX < 400) {
            currentPosition = position1;
        }

        if (currentPosition == RecyclerView.NO_POSITION) {
            return RecyclerView.NO_POSITION;
        }

        return currentPosition;
    }
}

Example:

LinearSnapHelper linearSnapHelper = new SnapHelperOneByOne();
linearSnapHelper.attachToRecyclerView(recyclerView);

Solution 2

This is late, i know.

There is a very simple way to get exactly the requested scrolling behaviour with the use of a custom SnapHelper.

Create your own SnapHelper by overwriting the standard one (android.support.v7.widget.LinearSnapHelper).

public class SnapHelperOneByOne extends LinearSnapHelper{

    @Override
    public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY){

        if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
            return RecyclerView.NO_POSITION;
        }

        final View currentView = findSnapView(layoutManager);

        if( currentView == null ){
            return RecyclerView.NO_POSITION;
        }

        final int currentPosition = layoutManager.getPosition(currentView);

        if (currentPosition == RecyclerView.NO_POSITION) {
            return RecyclerView.NO_POSITION;
        }

        return currentPosition;
    }
}

This is basicly the stadard method, but without adding a jump counter which gets calculated by scroll speed.

If you swipe fast and long, the next(or previous) view will be centered (shown).

If you swipe slow and short, the current centered view stays centered after release.

I hope this answer can still help anybody.

Solution 3

https://github.com/googlesamples/android-HorizontalPaging/

This has the link to something similar to what you have shown in the images. Let me know if there is something additional you are looking for, and I will link the relevant libraries.

Basically the difference between ViewPager and recyclerView is that, in recyclerView you are switching between many item, while in ViewPager you are switching between many fragments or independent pages itself.

I see you are using this https://github.com/lsjwzh/RecyclerViewPager, is there any particular use case you have in mind?

Share:
28,385
Akshay Bhat 'AB'
Author by

Akshay Bhat 'AB'

Love Programming.

Updated on March 19, 2020

Comments

  • Akshay Bhat 'AB'
    Akshay Bhat 'AB' about 4 years

    I tried adding on Scroll listener for recycler view and made some logic but i am not able to swipe one item at a time. I did some search on internet but i got some third party library which has custom recycler view. Can we implement one item swipe at a time in recycler view? If yes Please tell how? One item swipe at a time like this image.

  • Hoby
    Hoby over 6 years
    Thank you! your answer saved my day
  • Dheeraj Rijhwani
    Dheeraj Rijhwani about 6 years
    Is there is any method to increase speed of Swipe. It swipes but it will need full swipe to scroll to next item.
  • Rahul Khurana
    Rahul Khurana over 5 years
    had you find any way to increase speed of swipe?
  • ParSa
    ParSa almost 5 years
    You Are Genius Bro , Thanks
  • user924
    user924 over 4 years
    @DheerajRijhwani did you find a solution?
  • user924
    user924 over 4 years
    it's not very good, because you still need to scroll to the end
  • user924
    user924 over 4 years
    Why are there constants? 400
  • Amar Singh
    Amar Singh about 4 years
    not smooth to scroll
  • Amar Singh
    Amar Singh about 4 years
    nice but in case of vertical scroll velocityX replace with velocityY working for me. and mr user924 400 is used for check then velocity of scroll is below of more for check up scroll or down scroll
  • Bilal Kazi
    Bilal Kazi about 4 years
    Use the new PagerSnapHelper() rather than LinearShapHelper()
  • shivam chawla
    shivam chawla over 3 years
    loved your Answer buddy Thanks Saved my day bruh
  • G.N.SRIDHAR
    G.N.SRIDHAR over 3 years
    Its really worth. You saved my day bro. Thanks
  • Vijay
    Vijay over 3 years
    If you're using the reverse layout in recyclerView then change the position2 with position1 in the if-else clause. now you'll get the desired output.
  • Skovie
    Skovie over 3 years
    Thanks, it's perfect