我正在尝试在 recyclerview 上实现拉动刷新和无限滚动。虽然无限滚动效果很好,但如果我导航到 recylerview 的顶部并触发拉动刷新,但列表会刷新,它会导致无限滚动被禁用。我正在发布代码,任何帮助表示赞赏
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 );