38

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.

Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32
  • 1
    Viewpager is what you're looking for. As for custom transitions go through [this](http://developer.android.com/training/animation/screen-slide.html) – Udit Mukherjee Sep 01 '15 at 06:34
  • Thank you. But i cant use ViewPager for scrolling one item in Recyclerview. –  Sep 01 '15 at 06:46
  • Um, I meant use Viewpager instead of a RecyclerView. – Udit Mukherjee Sep 01 '15 at 06:48
  • Is there any other way without using Viewpager? –  Sep 01 '15 at 10:34
  • Duplicate question. Please refer this [link](http://stackoverflow.com/a/41988804/7182978). – Kshitij Jain Feb 11 '17 at 18:25
  • hello @AkshayBhat'AB' can you please help me with this https://stackoverflow.com/questions/49952965/recyclerview-horizontal-scrolling-to-left?noredirect=1#comment87836903_49952965 –  May 18 '18 at 12:09

3 Answers3

41

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);
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Vladimir Escoto
  • 413
  • 4
  • 5
33

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.

Palm
  • 699
  • 1
  • 6
  • 17
2

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?

Varun Agarwal
  • 1,587
  • 14
  • 29