【问题标题】:Scrolling recyclerview using buttons right and left使用左右按钮滚动回收视图
【发布时间】:2019-02-03 17:19:57
【问题描述】:

我想使用左右按钮滚动大约 10 个项目的项目

我尝试使用 scrollBy 但它不起作用

这是我的布局图像。

我希望能够左右滚动

【问题讨论】:

  • 请看我的回答
  • 您忘记发布代码了吗?

标签: java android android-recyclerview scroll


【解决方案1】:

您可以使用 下一步按钮:

mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findLastVisibleItemPosition() + 1);

对于上一个按钮:

mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findFirstVisibleItemPosition() - 1);

this

【讨论】:

  • 这一定是公认的答案……你救了我的命
【解决方案2】:

你可以有一个变量来跟踪RecyclerView的当前显示位置,在sn-ps下面将它命名为“currentPosition”

如果要导航到下一项,则按如下方式递增:

private void scrollToNext() {
    if ((recyclerView.getLayoutManager()) != null) {
        ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(currentPosition++, 0);
    }
}

您可以使用RelativeLayout 或自定义布局代替LinearLayout;根据您在RecyclerView 中使用的内容

同样,如果你想要前一个,就减少它

private void scrollToPrevious() {
    if ((recyclerView.getLayoutManager()) != null) {
        ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(currentPosition--, 0);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多