RecyclerView 对齐居中后如何获取居中项?

How to get the center item after RecyclerView snapped it to center?

我正在实施水平 RecyclerView 滚动后将项目对齐到中心,例如 Google 播放 App 水平列表。 This 是评论。

我的代码如下:

MainMenuAdapter mainMenuAdapter = new MainMenuAdapter(this, mDataset);

final LinearLayoutManager layoutManagaer = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView mainMenu = (RecyclerView) findViewById(R.id.main_menu);
mainMenu.setLayoutManager(layoutManagaer);
mainMenu.setAdapter(mainMenuAdapter);

final SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(mainMenu);     

如何在 RecyclerView 将其对齐到中心后获得中心项目(位置)?没有任何侦听器实现吗? 此外,当触摸项目视图时,我想将其对齐到中心。我该怎么做?

如需View,可致电

 View view =  snapHelper.findSnapView(layoutManagaer);

获得视图后,您应该能够获取该视图在数据集上的位置。例如使用

   mainMenu.getChildAdapterPosition(view)

最好用这个方法:
https://medium.com/over-engineering/detecting-snap-changes-with-androids-recyclerview-snaphelper-9e9f5e95c424

原文post:
即使您不打算使用 SnapHelper,您也可以通过 RecyclerView.OnScrollListener.

获得中心元素位置
  1. 复制 MiddleItemFinder class 到您的项目。
  2. 创建回调对象MiddleItemCallback.

    MiddleItemFinder.MiddleItemCallback callback = 
            new MiddleItemFinder.MiddleItemCallback() {
                    @Override
                    public void scrollFinished(int middleElement) {
                        // interaction with middle item
                    }
    };
    
  3. 为您的 RecyclerView

    添加新的滚动侦听器
    recyclerView.addOnScrollListener(
            new MiddleItemFinder(getContext(), layoutManager, 
                    callback, RecyclerView.SCROLL_STATE_IDLE));
    
  4. 最后一个参数或MiddleItemFinder构造函数是scrollState

    • RecyclerView.SCROLL_STATE_IDLE – RecyclerView 当前不是 滚动。滚动完成。
    • RecyclerView.SCROLL_STATE_DRAGGING – RecyclerView 当前是 被外部输入(例如用户触摸输入)拖动。
    • RecyclerView.SCROLL_STATE_SETTLING – RecyclerView 当前是 在不受外部控制的情况下动画到最终位置。
    • MiddleItemFinder.ALL_STATES – 所有州一起。
  5. 例如,如果您选择 RecyclerView.SCROLL_STATE_IDLE 作为最后一个构造函数参数,那么在所有滚动结束时回调对象将 return 您位于中间元素位置。

MiddleItemFinder class:

public class MiddleItemFinder extends RecyclerView.OnScrollListener {

    private
    Context context;

    private
    LinearLayoutManager layoutManager;

    private
    MiddleItemCallback callback;

    private
    int controlState;

    public
    static final int ALL_STATES = 10;

    public MiddleItemFinder(Context context, LinearLayoutManager layoutManager, MiddleItemCallback callback, int controlState) {
    this.context = context;
    this.layoutManager = layoutManager;
    this.callback = callback;
    this.controlState = controlState;
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

        if (controlState == ALL_STATES || newState == controlState) {

            int firstVisible = layoutManager.findFirstVisibleItemPosition();
            int lastVisible = layoutManager.findLastVisibleItemPosition();
            int itemsCount = lastVisible - firstVisible + 1;

            int screenCenter = context.getResources().getDisplayMetrics().widthPixels / 2;

            int minCenterOffset = Integer.MAX_VALUE;

            int middleItemIndex = 0;

            for (int index = 0; index < itemsCount; index++) {

                View listItem = layoutManager.getChildAt(index);

                if (listItem == null)
                    return;

                int leftOffset = listItem.getLeft();
                int rightOffset = listItem.getRight();
                int centerOffset = Math.abs(leftOffset - screenCenter) + Math.abs(rightOffset - screenCenter);

                if (minCenterOffset > centerOffset) {
                    minCenterOffset = centerOffset;
                    middleItemIndex = index + firstVisible;
                }
            }

            callback.scrollFinished(middleItemIndex);
        }
    }

    public interface MiddleItemCallback {

        void scrollFinished(int middleElement);
    }
}