This is for snapping an element in the center when scrolling or by clicking on ite.
You need to add SnapHelper to RecyclerView. Here's how:
final RecyclerView recyclerViewObject = view.findViewById(R.id.recyclerViewObjectId); final LinearSnapHelper snapHelper = new LinearSnapHelper(); snapHelper.attachToRecyclerView(recyclerViewObject); recyclerViewObject.setOnFlingListener(snapHelper);
then you just call this code
recyclerViewObject.addOnItemTouchListener( new RecyclerItemClickListener(getContext(), recyclerViewObject ,new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { recyclerViewObject.smoothScrollToPosition(position); } @Override public void onLongItemClick(View view, int position) { } }) );
source share