Recycler视图中的Android水平自动滚动 [英] Android Horizontal Auto scroll in Recycler view
问题描述
我在列表中有两个值,并使用Recycler视图在水平列表视图中显示该值.在这里,我需要自动滚动无限的水平列表.我尝试使用下面的代码,但没有结果.
I have two values in list and displaying that in horizontal list view using Recycler view. Here I need to auto scroll the horizontal list unlimited. I tried with the below code but no result.
> HorizontalScrollView:自动滚动到新时结束添加了视图?
推荐答案
请在此处检查解决方案. https://github.com/ritesh-bhavsar86/StockAutoScroll
please check the solution here. https://github.com/ritesh-bhavsar86/StockAutoScroll
首先创建可运行文件:
first create runnable:
final int duration = 10;
final int pixelsToMove = 30;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private final Runnable SCROLLING_RUNNABLE = new Runnable() {
@Override
public void run() {
rv_autoScroll.smoothScrollBy(pixelsToMove, 0);
mHandler.postDelayed(this, duration);
}
};
然后在setadapter()到recyclerView之后,使用以下命令:
then after setadapter() to the recyclerView use following:
rv_autoScroll.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastItem = layoutManager.findLastCompletelyVisibleItemPosition();
if(lastItem == layoutManager.getItemCount()-1){
mHandler.removeCallbacks(SCROLLING_RUNNABLE);
Handler postHandler = new Handler();
postHandler.postDelayed(new Runnable() {
@Override
public void run() {
rv_autoScroll.setAdapter(null);
rv_autoScroll.setAdapter(madapter);
mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
}
}, 2000);
}
}
});
mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
rv_autoScroll是recyclerview
rv_autoScroll is recyclerview
和
layoutmanager是设置为recyclerview的LayoutManager
layoutmanager is LayoutManager which set to recyclerview
这篇关于Recycler视图中的Android水平自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!