在我的应用中,我有一个包含20-30个项目的回收站视图,并且具有拉动以刷新内容的功能,并且我希望阻止用户在刷新过程中滚动视图,因为它会产生错误如果用户绑定滚动并正在进行重新刷新,但是我真的迷失了如何进行:/
eRecyclerView = findViewById(R.id.recycler_view);
RecyclerView.LayoutManager eLayoutManager = new GridLayoutManager(this, 2);
eRecyclerView.setLayoutManager(eLayoutManager);
swipeRefreshLayout = findViewById(R.id.refreshSecondLevel);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadNewContent();
Handler delay = new Handler();
delay.postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
}
}, 3000);
}
});
答案 0 :(得分:1)
您应该在RecyclerView.LayoutManager
上覆盖canScrollVertically
方法:
public class ToggledScrollLayoutManager extends LinearLayoutManager {
private boolean mIsScrollEnabled = true;
public ToggledScrollLayoutManager(Context context) {
super(context);
}
public void setScrollEnabled(boolean enabled) {
this.mIsScrollEnabled = enabled;
}
@Override
public boolean canScrollVertically() {
return mIsScrollEnabled && super.canScrollVertically();
}
}
然后在用户刷新页面时使用它:
recyclerView = findViewById(R.id.recyclerView);
ToggledScrollLayoutManager layoutManager = new ToggledScrollLayoutManager();
recyclerView.setLayoutManager(layoutManager);
swipeRefreshLayout = findViewById(R.id.refreshSecondLevel);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadNewContent();
layoutManager.setScrollEnabled(false);
Handler delay = new Handler();
delay.postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
layoutManager.setScrollEnabled(true);
}
}, 3000);
}
}
});