在嵌套的 recyclerview 中,子 recyclerview 的 GridLayoutManager.getChildCount() 给出总项目数

In nested recyclerview, child recyclerview's GridLayoutManager.getChildCount() gives total item count

我在 SwipeRefreshLayout 中使用了一个 RecyclerViewRecyclerView 还有另外 2 个 RecyclerView(目前;它可能会增加)。在我的第二个 RecyclerView 中,我试图实现无限滚动。但是我的 RecyclerView.getItemCount()RecyclerView.getChildCount() 给出了相同的值。此外,第二个 re GridLayoutManagerGridlayoutManager.findFirstVisibleItemPosition() 总是给出 0,而 GridLayoutManager.findLastVisibleItemPosition() 总是给出列表大小 - RecyclerViewOnScrolled 中的 1。是什么原因造成的,我应该怎么做才能实现无限滚动。

fragment.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");
            }
        }
};

抱歉回复晚了。但我会 post 我的解决方案在这里,以防万一有人在寻找它们。

在 parent 回收站视图的适配器中,我为视图设置了标签

@Override
public SectionRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mLayoutInflater.inflate(R.layout.view_section, parent, false);
    view.setTag(viewType);
    return new SectionRowHolder(view);
}

SectionRowHolder 是简单的 ViewHolder,带有 RecyclerView.OnScrollListener 属性 和 getter 和 setter。

public class SectionRowHolder extends RecyclerView.ViewHolder {

    protected RecyclerView recyclerView;
    RecyclerView.OnScrollListener mOnScrollListener;

    public SectionRowHolder(View view) {
        super(view);
        this.recyclerView = (RecyclerView) view.findViewById(R.id.section_list);

    }

    public RecyclerView.OnScrollListener getCustomScrollListener() {
        return mOnScrollListener;
    }

    public void setCustomScrollListener(RecyclerView.OnScrollListener mOnScrollListener) {
        this.mOnScrollListener = mOnScrollListener;
    }
}

然后在 onBindViewHolder 的 child 无限滚动中,我在滚动侦听器中实现了加载更多逻辑并设置为 child RecyclerView。

 RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {

        boolean loadEnable = false;

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            mTotalScrolled += dy;
            if ((mTotalScrolled + LOAD_MORE_ENABLE_HEIGHT) > recyclerView.getHeight() && loadEnable) {
                loadEnable = false;
                mLoadMoreListener.loadMore();
            } else {
                loadEnable = true;
            }
        }
    };
    holder.setCustomScrollListener(scrollListener);
    holder.recyclerView.addOnScrollListener(scrollListener);

此处 LOAD_MORE_ENABLE_HEIGHT 是从 child 回收器视图底部偏移以初始化 loadmore() 逻辑,mLoadMoreListener 是对片段的回调或 activity .

最终将 scoll 侦听器从 parent 回收器视图传递到 child 回收器视图,在我的 parent RecyclerViewonScrollListener

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            View v = mRecyclerView.findViewWithTag(CHILD_RECYCLERVIEW_TAG);
            SectionedLifeAtAdapter.SectionRowHolder viewHolder =
                    (SectionedLifeAtAdapter.SectionRowHolder) mRecyclerView
                            .findContainingViewHolder(v);
            if (viewHolder.getCustomScrollListener() != null)
                viewHolder.getCustomScrollListener().onScrolled((RecyclerView) v
                        .findViewById(R.id.section_list), dx, dy);

            Logger.e(TAG, ">>> call to on scrolled listener >>>");
        }
    });

这里的CHILD_RECYCLERVIEW_TAG是你在parent适配器的onCreateViewHolder中设置的视图类型。 它看起来有点乱,但它对我来说没有任何问题。