gpt4 book ai didi

android - RecyclerView 中类似寻呼机的行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:31 24 4
gpt4 key购买 nike

我正在尝试为水平 RecyclerView 实现类似 ViewPager 的行为。来自适配器的数据应该像往常一样膨胀和绑定(bind),但是通过 Recycler 的导航应该以不同的方式处理。当用户滑动(或尝试滚动)时,我将 Recycler 朝那个方向移动一个项目,并将其粘在左侧。

我已经有了所有的项目转换逻辑。我使用的是自定义 LayoutManager,它使用自定义 LinearSmoothScroller() 覆盖 onSmoothScrollToPosition(),使项目向左粘贴。

问题是 - 我如何覆盖 RecyclerView 的滚动行为来拦截滑动并自己处理它们?我尝试在 LayoutManager 中禁用滚动,然后在 onTouchListener 中拦截手势,但这似乎不起作用。 RecyclerView 框架是否有一种干净的方法来处理这个问题?

最佳答案

this 中涵盖的 native Android 解决方案post 是一种让 RecyclerView 表现得像 ViewPager 的 super 简单的方法。

相关位:

The 24.2.0 version of the support library introduced two new classes (SnapHelper and LinearSnapHelper) that should be used to handle snapping in a RecyclerView. .... The only code needed is:

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

SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
snapHelper.attachToRecyclerView(startRecyclerView);

关于android - RecyclerView 中类似寻呼机的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36329150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com