Step 1: import below class in your android project .
package com.example.utils;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
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;
}
}
Step 2: write below lines in your activity, fragment or class whare your recyclerview widget.
SnapHelper snapHelper = new SnapHelperOneByOne(); snapHelper.attachToRecyclerView(YOUR_RECYCLERVIEW);
Recent Comments
Recent Post
8 Financial Considerations of E-Commerce
- August 9, 2024
- 2 min read
Google announces 5 changes to for Mobile
- July 18, 2024
- 0 min read
Top 5 Ecommerce Platforms for Boosting Your
- July 16, 2024
- 2 min read