我想使用左右按钮滚动包含 10 个项目的项目我尝试使用 scrollBy 但它不起作用这是我的布局图像。我希望能够左右滚动
2 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
您可以有一个变量来跟踪 RecyclerView 的当前显示位置,在下面的代码片段中将其命名为“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);
}
}
添加回答
举报
0/150
提交
取消
