要从RecyclerView中获取屏幕中间的项,您可以将OnScrollListener附加到RecyclerView上,在侦听器内部,您应该获取当前项目的位置,然后检查给定项目的区域是否位于屏幕中间。
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
}
}
}
最终结果应该是这样的:
这是为了在滚动或单击项目时将其捕捉到屏幕中心。
您需要为RecyclerView添加SnapHelper。以下是方法:
final RecyclerView recyclerViewObject = view.findViewById(R.id.recyclerViewObjectId);
final LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerViewObject);
recyclerViewObject.setOnFlingListener(snapHelper);
然后您只需调用此代码
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) {
}
})
);
View view = rvCategory.getChildAt(pos);
if (view == null)
return;
int scrollX = (view.getLeft() - (StaticData.SCREEN_WIDTH / 2)) + (view.getWidth() / 2);
rvCategory.smoothScrollBy(scrollX, 0);
LinearLayoutManager layoutManager = ((LinearLayoutManager)recyclerView.getLayoutManager());
int totalVisibleItems = layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition()
int centeredItemPosition = totalVisibleItems / 2;
recyclerView.smoothScrollToPosition(position);
recyclerView.setScrollY(centeredItemPosition );
class CenterLayoutManager : LinearLayoutManager {
constructor(context: Context) : super(context)
constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State, position: Int) {
val centerSmoothScroller = CenterSmoothScroller(recyclerView.context)
centerSmoothScroller.targetPosition = position
startSmoothScroll(centerSmoothScroller)
}
private class CenterSmoothScroller(context: Context) : LinearSmoothScroller(context) {
override fun calculateDtToFit(viewStart: Int, viewEnd: Int, boxStart: Int, boxEnd: Int, snapPreference: Int): Int = (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2)
}
}
myRecyclerview.layoutManager =
CenterLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
myRecyclerview.smoothScrollToPosition(position)
val snapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(myRecyclerview)
Recyclerview.setOnScrollListener(object:
RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
var view=recyclerView[0]
}
})
ViewPager
替代 RecyclerView。 - Sanif SS