2

我正在使用RecyclerViewinside SwipeRefreshLayoutRecyclerView还有另外 2 个RecyclerView(现在;它可能会增加)。在我第二次RecyclerView尝试实现无限滚动。但我的RecyclerView.getItemCount()RecyclerView.getChildCount()给予相同的价值。2nd re 也GridLayoutManager总是GridlayoutManager.findFirstVisibleItemPosition()给出 0 并且GridLayoutManager.findLastVisibleItemPosition()总是给出列表大小 - 1 中OnScrolledRecyclerView. 是什么导致了这种情况,我应该怎么做才能实现无限滚动。

片段.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");
            }
        }
};
4

1 回答 1

1

这么晚才回复很抱歉。但是如果其他人正在寻找他们,我会在这里发布我的解决方案。

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

@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,在我父母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 是您在onCreateViewHolder父适配器中设置的视图类型。它看起来有点乱,但它为我完成了这项工作,没有任何问题。

于 2017-07-01T09:45:30.337 回答