[英]Horizontal Recycler view auto scrolling error
我有這個RecyclerView
作為自動收報機,一開始自動滾動工作正常,但一段時間后它變得奇怪(來回)並且RecyclerView
卡在一個項目上而不再平滑滾動,任何人都可以幫助我。
這是我的布局管理器:
LinearLayoutManager layoutManager = new LinearLayoutManager(HomeActivity.this, LinearLayoutManager.HORIZONTAL, false) {
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller smoothScroller = new LinearSmoothScroller(HomeActivity.this) {
private static final float SPEED = 5500f;// Change this value (default=25f)
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return SPEED / displayMetrics.densityDpi;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
};
rvTicker.setLayoutManager(layoutManager);
這是我的自動滾動功能:
public void autoScroll(){
speedScroll = 0;
handler = new Handler();
runnable = new Runnable() {
int count = 0;
@Override
public void run() {
if(count == tickerAdapter.getItemCount())
count = 0;
if(count < tickerAdapter.getItemCount()){
rvTicker.smoothScrollToPosition(++count);
handler.postDelayed(this,speedScroll);
}
}
};
handler.postDelayed(runnable,speedScroll);
}
解決了,我調整了autoscroll
功能:
public void autoScroll(){
speedScroll = 0;
handler = new Handler();
runnable = new Runnable() {
int count = 0;
@Override
public void run() {
if(count == tickerAdapter.getItemCount())
count = 0;
else {
if(count < tickerAdapter.getItemCount()){
rvTicker.smoothScrollToPosition(++count);
handler.postDelayed(this,speedScroll);
}else {
count = 0;
}
}
Log.wtf("tickerAdapter.getItemCount()", tickerAdapter.getItemCount()+"");
Log.wtf("count", count+"");
Log.wtf("++count", ++count+"");
Log.wtf("==============","=============");
}
};
handler.postDelayed(runnable,speedScroll);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.