一次刷一件物品Recyclerview [复制]

问题描述 投票:20回答:3

这个问题在这里已有答案:

我尝试在回收器视图上添加Scroll监听器并制作了一些逻辑,但我无法一次刷一个项目。我在互联网上做了一些搜索,但我得到了一些第三方库,它有定制的回收站视图。我们可以在回收站视图中一次实施一个项目滑动吗?如果是,请告诉我怎么做?一个项目一次刷卡像这个image

android android-fragments android-viewpager android-recyclerview
3个回答
14
投票

我知道,这已经很晚了。

有一种非常简单的方法可以使用自定义SnapHelper准确获取所请求的滚动行为。

通过覆盖标准的(android.support.v7.widget.LinearSnapHelper)创建自己的SnapHelper。

public class SnapHelperOneByOne extends LinearSnapHelper{

    @Override
    public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY){

        if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
            return RecyclerView.NO_POSITION;
        }

        final View currentView = findSnapView(layoutManager);

        if( currentView == null ){
            return RecyclerView.NO_POSITION;
        }

        final int currentPosition = layoutManager.getPosition(currentView);

        if (currentPosition == RecyclerView.NO_POSITION) {
            return RecyclerView.NO_POSITION;
        }

        return currentPosition;
    }
}

这基本上是标准方法,但没有添加通过滚动速度计算的跳转计数器。

如果快速和长按,下一个(或上一个)视图将居中(显示)。

如果您慢速和短暂滑动,当前居中的视图会在释放后保持居中。

我希望这个答案仍然可以帮助任何人。


1
投票

https://github.com/googlesamples/android-HorizontalPaging/

这与您在图像中显示的内容类似。如果您正在寻找其他内容,请告诉我,我将链接相关的库。

基本上,ViewPager和recyclerView之间的区别在于,在recyclerView中,您在许多项目之间切换,而在ViewPager中,您在许多片段或独立页面之间切换。

我看到你正在使用这个https://github.com/lsjwzh/RecyclerViewPager,你有什么特别的用例吗?


1
投票

这可以减轻物品之间的移动:

public class SnapHelperOneByOne extends LinearSnapHelper {

    @Override
    public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {

        if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
            return RecyclerView.NO_POSITION;
        }

        final View currentView = findSnapView(layoutManager);

        if (currentView == null) {
            return RecyclerView.NO_POSITION;
        }

        LinearLayoutManager myLayoutManager = (LinearLayoutManager) layoutManager;

        int position1 = myLayoutManager.findFirstVisibleItemPosition();
        int position2 = myLayoutManager.findLastVisibleItemPosition();

        int currentPosition = layoutManager.getPosition(currentView);

        if (velocityX > 400) {
            currentPosition = position2;
        } else if (velocityX < 400) {
            currentPosition = position1;
        }

        if (currentPosition == RecyclerView.NO_POSITION) {
            return RecyclerView.NO_POSITION;
        }

        return currentPosition;
    }
}

例:

LinearSnapHelper linearSnapHelper = new SnapHelperOneByOne();
linearSnapHelper.attachToRecyclerView(recyclerView);
© www.soinside.com 2019 - 2024. All rights reserved.