Reputation: 132
In my app I have a recycler view with 20 - 30 items and I have the function to pull to refresh the content, and I want to prevent the user from scrolling the view while the refresh is in progress because it creates an error if the user ties to scroll and the regresh is in progress, but I'm really lost on how to do it :/
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);
}
});
Upvotes: 2
Views: 196
Reputation: 5608
You should override canScrollVertically
method on your RecyclerView.LayoutManager
:
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();
}
}
Then use it when user refreshed the page:
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);
}
}
});
Upvotes: 1