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

时间:2016-08-01 05:08:38

标签: android android-recyclerview nestedrecyclerview

我在RecyclerView内使用了SwipeRefreshLayoutRecyclerView还有另外2 RecyclerView(现在;它可能会增加)。在我的第二个RecyclerView我试图实现无限滚动。但我的RecyclerView.getItemCount()RecyclerView.getChildCount()给出了相同的价值。此外,第二个重新设置为GridLayoutManagerGridlayoutManager.findFirstVisibleItemPosition()始终为0,GridLayoutManager.findLastVisibleItemPosition()始终提供列表大小 - OnScrolled RecyclerView中的列表大小为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 for child recycler view

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

1 个答案:

答案 0 :(得分:0)

很抱歉迟到的回复。但是我会在这里发布我的解决方案,以防其他人正在寻找它们。

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

@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中,我已经在滚动侦听器中实现了加载更多逻辑并设置为子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偏离子回收器视图的底部以初始化loadmore()逻辑,mLoadMoreListener回调到片段或活动。

最后将scoll侦听器从父级回收站视图传递给子回收器vew,在我的父RecyclerView的{​​{1}}

onScrollListener

此处CHILD_RECYCLERVIEW_TAG是您在父适配器的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 >>>"); } }); 中设置的视图类型。 它有点乱,但它没有任何问题为我做了工作。