change RecyclerView item to be above all others

好久不见. 提交于 2019-12-25 02:16:05

问题


I have horizontal recyclerView that contains images as items. How can I detect when a RecycleView item is in the center of the screen and emphasize it to be something like this :

Example :


回答1:


You could use the carousel logic with RecyclerView combination and SnapHelper compatibility class like this:

<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" />

Then attach your recyclerView with SnapHelper class:

LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);

And provide the logic for currently selected centered item:

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);
    }
});


来源:https://stackoverflow.com/questions/50760805/change-recyclerview-item-to-be-above-all-others

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!