我在列表中有兩個值,並使用Recycler視圖在水平列表視圖中顯示。在這裏,我需要自動滾動水平列表無限。 我試着用下面的代碼但沒有結果。Android臥式在Recycler視圖中自動滾動
HorizontalScrollView: auto-scroll to end when new Views are added?
答覆讚賞...
我在列表中有兩個值,並使用Recycler視圖在水平列表視圖中顯示。在這裏,我需要自動滾動水平列表無限。 我試着用下面的代碼但沒有結果。Android臥式在Recycler視圖中自動滾動
HorizontalScrollView: auto-scroll to end when new Views are added?
答覆讚賞...
請在這裏查看解決方案。 https://github.com/ritesh-bhavsar86/StockAutoScroll
首先創建可運行:
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使用後下列:
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
和
LayoutManager是其中的LayoutManager設置爲recyclerview