首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在网格化管理器视图中离散滚动?

如何在网格化管理器视图中离散滚动?
EN

Stack Overflow用户
提问于 2018-11-23 09:17:13
回答 1查看 172关注 0票数 0

我使用recyclerView和GridLayoutManager,并将其设置为水平滚动。我要它小心滚动。当我启动它时,它就是这样显示的。

当我滚动它一点点,它应该继续滚动,直到下一个项目是完全可见的,也就是谨慎地,而不是在此之前停止。应该是这样的。

结局应该是这样。

我怎么能像这样谨慎地滚动?如有任何指导或帮助,将不胜感激!

EN

Stack Overflow用户

回答已采纳

发布于 2018-11-23 10:12:50

我在我的项目中使用了SnapHelper,只需在util包中创建一个类就可以这样做。

代码语言:javascript
运行
复制
public class MySnapHelper extends LinearSnapHelper {

private OrientationHelper verticalHelper, horizontalHelper;

public MySnapHelper() {

}

@Override
public void attachToRecyclerView(@Nullable RecyclerView recyclerView)
        throws IllegalStateException {
    super.attachToRecyclerView(recyclerView);
}

@Override
public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager,
                                          @NonNull View targetView) {
    int[] out = new int[2];

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

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

@Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) {

    if (layoutManager instanceof LinearLayoutManager) {

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

    return super.findSnapView(layoutManager);
}

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

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

    if (layoutManager instanceof LinearLayoutManager) {
        int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();

        boolean isLastItem = ((LinearLayoutManager) layoutManager)
                .findLastCompletelyVisibleItemPosition()
                == layoutManager.getItemCount() - 1;

        if (firstChild == RecyclerView.NO_POSITION || 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.getItemCount() - 1) {
                return null;
            } else {
                return layoutManager.findViewByPosition(firstChild + 1);
            }
        }
    }

    return super.findSnapView(layoutManager);
}

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

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

}

然后把它和你的回收视图贴在一起:

代码语言:javascript
运行
复制
SnapHelper mySnapHelper = new MySnapHelper ();
mySnapHelper.attachToRecyclerView(mRecyclerView);
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53443695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档