How to set an item in the Recycler viewing center if selected

I am using RecyclerView to display elements horizontally. I want the selected item to be in the center of the view, like this.

enter image description here .

Here is how I do it:

 LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); recyclerView.setLayoutManager(layoutManager); 
+14
source share
3 answers

To get the middle element on your screen from the RecyclerView, you can attach the OnScrollListener to the RecyclerView, and inside the listener you need to get the position of the current elements and then check if the area of ​​this element is in the middle of the screen.

Example code in Kotlin:

 // Attach OnScrollListener to your RecyclerView addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { recyclerView.post { selectMiddleItem() } } }) 
 // implementation of method that is called from OnScrollListener private fun selectMiddleItem() { val firstVisibleIndex = layoutManager.findFirstVisibleItemPosition() val lastVisibleIndex = layoutManager.findLastVisibleItemPosition() val visibleIndexes = listOf(firstVisibleIndex..lastVisibleIndex).flatten() for (i in visibleIndexes) { val vh = findViewHolderForLayoutPosition(i) if (vh?.itemView == null) { continue } val location = IntArray(2) vh.itemView.getLocationOnScreen(location) val x = location[0] val halfWidth = vh.itemView.width * .5 val rightSide = x + halfWidth val leftSide = x - halfWidth val isInMiddle = screenWidth * .5 in leftSide..rightSide if (isInMiddle) { // "i" is your middle index and implement selecting it as you want // optionsAdapter.selectItemAtIndex(i) return } } } 

And as a result, you should get something like this:

enter image description here

+6
source

Please try the following:

 LinearLayoutManager layoutManager = ((LinearLayoutManager)recyclerView.getLayoutManager()); int totalVisibleItems = layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition() int centeredItemPosition = totalVisibleItems / 2; recyclerView.smoothScrollToPosition(position); recyclerView.setScrollY(centeredItemPosition ); 

Hope this helps.

+2
source

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) { } }) ); 
+2
source

All Articles