java - 从中心项开始我的 RecyclerView 水平旋转木马

标签 java android android-recyclerview android-scroll linearlayoutmanager

我正在创建一个旋转木马水平 RecyclerView,从 RecyclerView 的第一个项目开始,对焦点项目进行缩放。

自定义 CenterZoomLayoutManager 的代码:

public class CenterZoomLayoutManager extends LinearLayoutManager {
private final float mShrinkAmount = 0.15f;
private final float mShrinkDistance = 0.9f;

public CenterZoomLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
}

@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
    int orientation = getOrientation();
    if (orientation == HORIZONTAL) {

        int scrolled = super.scrollHorizontallyBy(dx, recycler, state);
        float midpoint = getWidth() / 2.f;
        float d0 = 0.f;
        float d1 = mShrinkDistance * midpoint;
        float s0 = 1.f;
        float s1 = 1.f - mShrinkAmount;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            float childMidpoint =
                    (getDecoratedRight(child) + getDecoratedLeft(child)) / 2.f;
            float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
            float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
            child.setScaleX(scale);
            child.setScaleY(scale);
        }
        return scrolled;
    } else return 0;
}

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    super.onLayoutChildren(recycler, state);
    scrollHorizontallyBy(0, recycler, state);
}
}

在我的 Fragment 中,我有以下内容:

private void onSetRecyclerView() {

    recyclerView = fragmentView.findViewById(R.id.recyclerView);

    if (recyclerView != null) {
        RecyclerView.LayoutManager layoutManager = new CenterZoomLayoutManager(context, LinearLayoutManager.HORIZONTAL,
                false);

        recyclerView.scrollToPosition(storeList.size() / 2);

        final SnapHelper snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(recyclerView);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }
}

我想从 center item 而不是开始位置开始我的 RecyclerView。

罪魁祸首:
在我的 CenterZoomLayoutManager 中,我通过 scrollHorizo​​ntallyBy(0, recycler, state) 将像素偏移开始设置为 0 像素

问题:
我找不到将 recyclerView.scrollToPosition(storeList.size()/2) 创建的偏移像素传递给 CenterZoomLayoutManager 的方法。我试图搜索不同的内置方法来获取 X 偏移量,但到目前为止还没有成功。

最佳答案

解决方案是更改 LinearSnapHelper 附加到 RecyclerView 的时间。以下是重新设计的 onSetRecyclerView(),它将 RecyclerView 的中心项目捕捉到屏幕的中心。请注意,在 RecyclerView 被布局并适当滚动之前,LinearSnapHelper 不会被附加。您不需要在 onLayoutChildren() 中进行任何滚动。

private void onSetRecyclerView() {
    recyclerView = findViewById(R.id.recyclerView);
    CenterZoomLayoutManager layoutManager =
        new CenterZoomLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
    // Scroll to the position we want to snap to
    layoutManager.scrollToPosition(storeList.size() / 2);
    // Wait until the RecyclerView is laid out.
    recyclerView.post(new Runnable() {
        @Override
        public void run() {
            // Shift the view to snap  near the center of the screen.
            // This does not have to be precise.
            int dx = (recyclerView.getWidth() - recyclerView.getChildAt(0).getWidth()) / 2;
            recyclerView.scrollBy(-dx, 0);
            // Assign the LinearSnapHelper that will initially snap the near-center view.
            LinearSnapHelper snapHelper = new LinearSnapHelper();
            snapHelper.attachToRecyclerView(recyclerView);
        }
    });
}

我的测试应用的初始屏幕是这样显示的。有 201 个“商店”。

enter image description here

关于java - 从中心项开始我的 RecyclerView 水平旋转木马,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52317646/

相关文章:

java - Activity崩溃生命周期方法——android

android - 改造缓存数据以供离线使用

java - 盒装与原始类型作为实体 id

java - 使用 kryo 注册类(class)的策略

android - "::"在 kotlin 中是什么意思?

android - 基于 bool 值更改适配器类中回收器 View 中按钮的图标

android - 在 ConstraintLayout 中使用 RecyclerView 时,所有 RecyclerView 项都不可见

java - 如何更改抽屉导航中的图标大小

java - 如何从linkedList中递归删除一个项目?

android - 像android中的常规一样工作的不可见/透明按钮?