我正在使用RecyclerView
inside SwipeRefreshLayout
。RecyclerView
还有另外 2 个RecyclerView
(现在;它可能会增加)。在我第二次RecyclerView
尝试实现无限滚动。但我的RecyclerView.getItemCount()
和RecyclerView.getChildCount()
给予相同的价值。2nd re 也GridLayoutManager
总是GridlayoutManager.findFirstVisibleItemPosition()
给出 0 并且GridLayoutManager.findLastVisibleItemPosition()
总是给出列表大小 - 1 中OnScrolled
的RecyclerView
. 是什么导致了这种情况,我应该怎么做才能实现无限滚动。
片段.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="@dimen/padding_standard"
android:paddingBottom="@dimen/padding_standard">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
parent_recyclerview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginBottom="@dimen/padding_standard"
android:orientation="vertical">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/padding_standard"
android:layout_marginBottom="@dimen/margin_standard"
android:textColor="@color/label_text"
android:textSize="@dimen/text_size_standard"
android:textStyle="bold"
tools:text="MOments"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/section_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"/>
<ProgressBar
android:id="@+id/events_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
</LinearLayout>
子回收器视图的 onScrollListener
RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
int itemSize = manager.getItemCount();
int firstVisibleItem = manager.findFirstVisibleItemPosition();
int visibleChIldCount = manager.getChildCount();
Logger.e(TAG,"=============== START =====================");
Logger.e(TAG, "itemSize: " + itemSize);
Logger.e(TAG, "firstVisibleitem: " + firstVisibleItem);
Logger.e(TAG, "visibleChIldCount: " + visibleChIldCount);
Logger.e(TAG,"mLayoutManager.firstCOmpletely: "+ manager.findFirstCompletelyVisibleItemPosition());
Logger.e(TAG,"mLayoutManager. lastcompletey: "+ manager.findLastCompletelyVisibleItemPosition());
Logger.e(TAG,"mLayoutManager.findLastVisible: "+ manager.findLastVisibleItemPosition());
Logger.e(TAG,"=================END ================");
if (itemSize >= firstVisibleItem + visibleChIldCount){
Logger.e("", "loading");
mLoadMoreListener.loadMore();
} else {
Logger.e(TAG, "not Loading");
}
}
};