How Disable Swiperefreshlayout When Recyclerview Is Not On The First Item?
My android project includes a recyclerView that contains a list of cardViews, and also there is a swipeRefreshLayout on the top of this recyclerView. When I scroll down the list an
Solution 1:
Maybe I am late, but have a try to this solution:
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mLayoutManager = newLinearLayoutManager(getActivity()); // a LinearLayoutManager
mRecyclerView.setLayoutManager(mLayoutManager); // setting layoutManager on our RecyclerView// Adding ScrollListener to getting whether we're on First Item position or not
mRecyclerView.addOnScrollListener(newRecyclerView.OnScrollListener() {
@OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
mSwipeRefreshLayout.setEnabled(mLinearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0); // 0 is for first item position
}
});
mSwipeRefreshLayout
is your SwipeRefreshLayout
After putting above code, you'll be able to swipe only when your First item is visible.
Hope this helps! 😊
Solution 2:
Here is the fix:
publicclassSwipeRefreshLayoutToggleScrollListenerextendsRecyclerView.OnScrollListener {
private List<RecyclerView.OnScrollListener> mScrollListeners = newArrayList<RecyclerView.OnScrollListener>();
privateintmExpectedVisiblePosition=0;
private SwipeRefreshLayout mSwipeLayout;
publicSwipeRefreshLayoutToggleScrollListener(SwipeRefreshLayout swipeLayout) {
mSwipeLayout = swipeLayout;
}
publicvoidaddScrollListener(RecyclerView.OnScrollListener listener){
mScrollListeners.add(listener);
}
publicbooleanremoveScrollListener(RecyclerView.OnScrollListener listener){
return mScrollListeners.remove(listener);
}
publicvoidsetExpectedFirstVisiblePosition(int position){
mExpectedVisiblePosition = position;
}
@OverridepublicvoidonScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
notifyScrollStateChanged(recyclerView,newState);
LinearLayoutManagerllm= (LinearLayoutManager) recyclerView.getLayoutManager();
intfirstVisible= llm.findFirstCompletelyVisibleItemPosition();
if(firstVisible != RecyclerView.NO_POSITION)
mSwipeLayout.setEnabled(firstVisible == mExpectedVisiblePosition);
}
@OverridepublicvoidonScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
notifyOnScrolled(recyclerView, dx, dy);
}
privatevoidnotifyOnScrolled(RecyclerView recyclerView, int dx, int dy){
for(RecyclerView.OnScrollListener listener : mScrollListeners){
listener.onScrolled(recyclerView, dx, dy);
}
}
privatevoidnotifyScrollStateChanged(RecyclerView recyclerView, int newState){
for(RecyclerView.OnScrollListener listener : mScrollListeners){
listener.onScrollStateChanged(recyclerView, newState);
}
}
}
for more info check this
URL:https://gist.github.com/NikolaDespotoski/1a6bb83dbae133f67812
Post a Comment for "How Disable Swiperefreshlayout When Recyclerview Is Not On The First Item?"