I am trying to implement pull to refresh and endless scroll on recyclerview. While the endless scroll works very well, if I navigate to the top of recylerview and trigger pull to refresh, list is refreshed though, it results in endless scroll disabled. I am posting the code, any help appreciated
Layout XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_confirmedListRowtoday"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/adapter_item_confirmed"
/>
</android.support.v4.widget.SwipeRefreshLayout>
<com.victor.loading.rotate.RotateLoading
android:id="@+id/rotateloading"
app:loading_color="@color/primary_light"
app:loading_width="5dp"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
/>
<TextView
android:id="@+id/emptyTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="No Appointments"
android:textColor="@color/black_semi_transparent"
android:textSize="20sp"
android:visibility="gone"
tools:visibility="visible"/>
</RelativeLayout>
ScrollListener implementation :
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = mRecyclerView.getChildCount();
totalItemCount = llm.getItemCount();
firstVisibleItem = llm.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
pageNumber++;
callAppointmentApi();
loading = true;
}
}
};
RecyclerView code :
mRecyclerView.setHasFixedSize(true);
llm = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(llm);
adapter = new ConfirmedRecyclerAdapter(getActivity(), appointmentModelList);
mRecyclerView.setAdapter(adapter);
// Setup refresh listener which triggers new data loading
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Your code to refresh the list here.
// Make sure you call swipeContainer.setRefreshing(false)
// once the network request has completed successfully.
appointmentModelList.clear();
pageNumber = 0;
callAppointmentApi();
mRecyclerView.addOnScrollListener( onScrollListener );
}
});
mRecyclerView.addOnScrollListener( onScrollListener );
This is my solution :