如何使用 SnapHelper 将捕捉位置从 RecycleView 的中心移动到左侧?

How to move the snap position from center to left of RecycleView using SnapHelper?

我有一个包含 ImageView 的 RecycleView,我的问题是如何将 snap 移动到 RecycleView 的左侧而不是中心?

当我移动 ImageView 时,它们会在中心对齐,我可以通过覆盖 CalculateDistanceToFinalSnap 方法将它们移动到 "snap window" 内的左侧或右侧。我想我现在需要将 "snap window" 移到 RecycleView 的左侧,但我不知道该怎么做,或者也许还有其他方法,请帮忙。

这是我的问题的图片,也许它会帮助你更清楚地理解: image

我已经实现了这个功能,我们只需要创建一个class和extent class LinearSnapHelper并覆盖方法CalculateDistanceToFinalSnapFindSnapView。您可以查看完整的演示 here .

主要代码如下:

 public class StartSnapHelper: LinearSnapHelper
 {
    private OrientationHelper mVerticalHelper, mHorizontalHelper;

    public StartSnapHelper()
    {
    }

    public override void AttachToRecyclerView(RecyclerView recyclerView)
    {
        base.AttachToRecyclerView(recyclerView);
    }

    public override int[] CalculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager, View targetView)
    {
        //return base.CalculateDistanceToFinalSnap(layoutManager, targetView);
        int[] outer = new int[2];

        if (layoutManager.CanScrollHorizontally())
        {
            outer[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager));
        } else {
            outer[0] = 0;
        }

    if (layoutManager.CanScrollVertically()) {
            outer[1] = distanceToStart(targetView, getVerticalHelper(layoutManager));
    } else {
            outer[1] = 0;
    }
    return outer;
    }

    private int distanceToStart(View targetView, OrientationHelper helper)
    {
        return helper.GetDecoratedStart(targetView) - helper.StartAfterPadding;
    }

    public override View FindSnapView(RecyclerView.LayoutManager layoutManager)
    {
        if (layoutManager is LinearLayoutManager) {

            if (layoutManager.CanScrollHorizontally())
            {
                return getStartView(layoutManager, getHorizontalHelper(layoutManager));
            }
            else
            {
                return getStartView(layoutManager, getVerticalHelper(layoutManager));
            }
        }

        return base.FindSnapView(layoutManager);
    }

    private View getStartView(RecyclerView.LayoutManager layoutManager,
                          OrientationHelper helper)
    {

        if (layoutManager is LinearLayoutManager) {
            int firstChild = ((LinearLayoutManager)layoutManager).FindFirstVisibleItemPosition();

            bool isLastItem = ((LinearLayoutManager)layoutManager)
                    .FindLastCompletelyVisibleItemPosition()
                    == layoutManager.ItemCount - 1;

            if (firstChild == RecyclerView.NoPosition || isLastItem)
            {
                return null;
            }

            View child = layoutManager.FindViewByPosition(firstChild);

            if (helper.GetDecoratedEnd(child) >= helper.GetDecoratedMeasurement(child) / 2
                    && helper.GetDecoratedEnd(child) > 0)
            {
                return child;
            }
            else
            {
                if (((LinearLayoutManager)layoutManager).FindLastCompletelyVisibleItemPosition()
                        == layoutManager.ItemCount - 1)
                {
                    return null;
                }
                else
                {
                    return layoutManager.FindViewByPosition(firstChild + 1);
                }
            }
        }
        return base.FindSnapView(layoutManager);
    }


    private OrientationHelper getVerticalHelper(RecyclerView.LayoutManager layoutManager)
    {
        if (mVerticalHelper == null)
        {
            mVerticalHelper = OrientationHelper.CreateVerticalHelper(layoutManager);
        }
        return mVerticalHelper;
    }

    private OrientationHelper getHorizontalHelper(RecyclerView.LayoutManager layoutManager)
    {
        if (mHorizontalHelper == null)
        {
            mHorizontalHelper = OrientationHelper.CreateHorizontalHelper(layoutManager);
        }
        return mHorizontalHelper;
    }
}

并像这样使用:

  SnapHelper snapHelperStart = new StartSnapHelper();
  snapHelperStart.AttachToRecyclerView(recyclerView);

@Jessie Zhang -MSFT 的解决方案对我有用。代码的格式有点奇怪,我很难把它拿过来。这是 Kotlin 中的相同解决方案(仅适用于水平捕捉)。

class StartSnapHelper: LinearSnapHelper() {
    override fun calculateDistanceToFinalSnap(layoutManager: RecyclerView.LayoutManager, targetView: View): IntArray? {
        return if (layoutManager.canScrollHorizontally()) {
            val outer = mutableListOf<Int>()
            outer.add(distanceToStart(targetView, getHorizontalHelper(layoutManager)))
            outer.add(0)

            outer.toIntArray()
        } else {
            super.calculateDistanceToFinalSnap(layoutManager, targetView)
        }
    }

    override fun findSnapView(layoutManager: RecyclerView.LayoutManager?): View? {
        return if (layoutManager is LinearLayoutManager) {
            if (layoutManager.canScrollHorizontally()) {
            getStartView(layoutManager, getHorizontalHelper(layoutManager))
            } else {
                super.findSnapView(layoutManager)
            }
        } else {
            super.findSnapView(layoutManager)
        }
    }

    private fun distanceToStart(targetView: View, helper: OrientationHelper): Int {
        return helper.getDecoratedStart(targetView) - helper.startAfterPadding
    }

    private fun getStartView(layoutManager: RecyclerView.LayoutManager, orientationHelper: OrientationHelper): View? {
        val firstChild = (layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
        val isLastItem = (layoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.itemCount - 1)

        if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
            return null
        }

        val child = layoutManager.findViewByPosition(firstChild)

        return if (orientationHelper.getDecoratedEnd(child) >= orientationHelper.getDecoratedMeasurement(child) / 2
        && orientationHelper.getDecoratedEnd(child) > 0) {
        child;
        } else {
            if (layoutManager.findFirstCompletelyVisibleItemPosition() == layoutManager.itemCount -1) {
                null
            } else {
                layoutManager.findViewByPosition(firstChild + 1)
            }
        }
    }

    private fun getHorizontalHelper(layoutManager: RecyclerView.LayoutManager): OrientationHelper {
        return OrientationHelper.createHorizontalHelper(layoutManager)
    }
}