簡體   English   中英

在嵌套的recyclerview中,child recyclerview的GridLayoutManager.getChildCount()給出了項目總數

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

我使用的是RecyclerView內部SwipeRefreshLayout .The RecyclerView有另一個2 RecyclerView (現在;它可能會增加)。 在我的第二個RecyclerView我試圖實現無限滾動。 但我的RecyclerView.getItemCount()RecyclerView.getChildCount()給出了相同的值。 第二個重要的是GridLayoutManagerGridlayoutManager.findFirstVisibleItemPosition()總是給出0並且GridLayoutManager.findLastVisibleItemPosition()總是給出列表大小 - 在RecyclerView OnScrolled為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");
            }
        }
};

這么晚才回復很抱歉。 但我會在這里發布我的解決方案,以防萬一其他人正在尋找他們。

在父回收器視圖的適配器中,我為視圖設置了標記

@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,帶有帶getter和setter的RecyclerView.OnScrollListener屬性。

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中設置的視圖類型。 它有點亂,但它沒有任何問題為我做了工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM