Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe one item at a time Recyclerview [duplicate]

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.

like image 346
Akshay Bhat 'AB' Avatar asked Sep 01 '15 06:09

Akshay Bhat 'AB'


Video Answer


2 Answers

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); 
like image 177
Vladimir Escoto Avatar answered Sep 20 '22 16:09

Vladimir Escoto


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.

like image 30
Palm Avatar answered Sep 17 '22 16:09

Palm