当scrollState不是IDLE时,如何避免滚动RecyclerView?
我尝试了一些想法,例如在canScrollHorizontally()
中覆盖LinearLayoutManager
,并在false
或同一概念应用于scrollState != IDLE
dispatchTouchEvent()
时返回Activity
},但它会永远停留在屏幕上。
我也尝试了一个Asyntask方法,但是while循环让它永远不会逃脱doInBackground并转到onPostExercute:
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (previousScrollState != 0) {
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
while (previousScrollState != 0) {
Log.i(TAG, "doInBackground " + previousScrollState);
}
return true;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
recList.setTouchEnabled(false);
}
@Override
protected void onPostExecute(Boolean aVoid) {
super.onPostExecute(aVoid);
recList.setTouchEnabled(true);
Log.i(TAG, "onPostExecute " + previousScrollState);
}
}.execute();
}
previousScrollState = newState;
}
我的目的是避免用户跳过任何页面(同时滚动两次或更多次)。我怎样才能实现它?谢谢分享。
答案 0 :(得分:0)
不确定这是否是一个糟糕的解决方案,但在得到更好的答案之前:
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (previousScrollState != RecyclerView.SCROLL_STATE_IDLE) {
if (asyncCheckScrollState == null || asyncCheckScrollState.isCancelled()) {
asyncCheckScrollState = new AsyncCheckScrollState();
asyncCheckScrollState.execute();
}
}
// ...
previousScrollState = newState;
}
和
public class AsyncCheckScrollState extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
recList.setTouchEnabled(false);
}
@Override
protected Void doInBackground(Void... params) {
try {
while (previousScrollState != 0) {
if (isCancelled()) {
Log.i(TAG, "canceled ");
break;
}
}
} catch (Exception e) {
} finally {
this.cancel(true);
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
recList.setTouchEnabled(true);
}
}