我有包含图像作为项目的水平RecyclerView。 如何检测RecycleView项目何时位于屏幕中央并强调它是这样的:
示例:
答案 0 :(得分:1)
您可以将轮播逻辑与RecyclerView组合和SnapHelper兼容性类一起使用,如下所示:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadingEdge="horizontal"
android:overScrollMode="never"
android:requiresFadingEdge="horizontal" />
然后将您的recyclerView附加到SnapHelper类:
LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
并提供当前所选中心项目的逻辑:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
float pos = (float) recyclerView.computeHorizontalScrollOffset() / (float) itemHeight;
int itemPos = (int) Math.floor(pos);
}
super.onScrollStateChanged(recyclerView, newState);
}
});