RecyclerView 水平滚动对齐居中 [英] RecyclerView horizontal scroll snap in center

查看:29
本文介绍了RecyclerView 水平滚动对齐居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RecyclerView 在此处制作类似轮播的视图,我希望该项目在滚动时对齐屏幕中间,一次一个项目.我试过使用 recyclerView.setScrollingTouchSlop(RecyclerView.TOUCH_SLOP_PAGING);

I'm trying to make a carousel-like view here using RecyclerView, I want the item to snap in the middle of the screen when scrolling, one item at a time. I've tried using recyclerView.setScrollingTouchSlop(RecyclerView.TOUCH_SLOP_PAGING);

但是视图仍然平滑滚动,我也尝试使用滚动侦听器实现我自己的逻辑,如下所示:

but the view is still scrolling smoothly, I've also tried to implement my own logic using scroll listener like so:

recyclerView.setOnScrollListener(new OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    Log.v("Offset ", recyclerView.getWidth() + "");
                    if (newState == 0) {
                        try {
                               recyclerView.smoothScrollToPosition(layoutManager.findLastVisibleItemPosition());
                                recyclerView.scrollBy(20,0);
                            if (layoutManager.findLastVisibleItemPosition() >= recyclerView.getAdapter().getItemCount() - 1) {
                                Beam refresh = new Beam();
                                refresh.execute(createUrl());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

从右向左滑动现在工作正常,但反过来不行,我在这里错过了什么?

The swipe from right to left is working fine now, but not the other way around, what am I missing here ?

推荐答案

With LinearSnapHelper,现在这很容易.

With LinearSnapHelper, this is now very easy.

您需要做的就是:

SnapHelper helper = new LinearSnapHelper();
helper.attachToRecyclerView(recyclerView);

更新

自 25.1.0 起可用,PagerSnapHelper 可以帮助实现类似ViewPager 的效果.像使用 LinearSnapHelper 一样使用它.

Available since 25.1.0, PagerSnapHelper can help achieve ViewPager like effect. Use it as you would use the LinearSnapHelper.

旧的解决方法:

如果你希望它的行为类似于 ViewPager,试试这个:

If you wish for it to behave akin to the ViewPager, try this instead:

LinearSnapHelper snapHelper = new LinearSnapHelper() {
    @Override
    public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
        View centerView = findSnapView(layoutManager);
        if (centerView == null) 
            return RecyclerView.NO_POSITION;

        int position = layoutManager.getPosition(centerView);
        int targetPosition = -1;
        if (layoutManager.canScrollHorizontally()) {
            if (velocityX < 0) {
                targetPosition = position - 1;
            } else {
                targetPosition = position + 1;
            }
        }

        if (layoutManager.canScrollVertically()) {
            if (velocityY < 0) {
                targetPosition = position - 1;
            } else {
                targetPosition = position + 1;
            }
        }

        final int firstItem = 0;
        final int lastItem = layoutManager.getItemCount() - 1;
        targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem));
        return targetPosition;
    }
};
snapHelper.attachToRecyclerView(recyclerView);

上面的实现只是根据速度的方向返回当前项目旁边的位置(居中),而不管大小.

The implementation above just returns the position next to the current item (centered) based on the direction of the velocity, regardless of the magnitude.

前者是包含在支持库版本 24.2.0 中的第一方解决方案.这意味着您必须将此添加到您的应用模块的 build.gradle 或更新它.

The former one is a first party solution included in the Support Library version 24.2.0. Meaning you have to add this to your app module's build.gradle or update it.

compile "com.android.support:recyclerview-v7:24.2.0"

这篇关于RecyclerView 水平滚动对齐居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆