如何在NestedScrollView中使用无限滚动的RecyclerView?

6
屏幕高度不够,因为一个屏幕上有许多包含两个RecyclerView的项目。所以我尝试在NestedScrollView中添加子视图。然而,所有项目一次加载完毕,或者回收站没有被回收。
所以我阅读了其他文章并尝试过,但对我没有用。
例如,添加app:layout_behavior="@string/appbar_scrolling_view_behavior"mRecyclerView.setNestedScrollingEnabled(false); 如果您知道如何做,请告诉我。谢谢阅读。
这是我的Java代码。
myDataset = new ArrayList<>();
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.map_recycler_view);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(mcontext);
    mLayoutManager.setAutoMeasureEnabled(true);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MyAdapter(this);
    mAdapter.setLinearLayoutManager(mLayoutManager);
    mAdapter.setRecyclerView(mRecyclerView);
    mRecyclerView.setAdapter(mAdapter);

以下是我的XML代码。

这是我的XML代码。

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="180dp">

            <fragment
                android:id="@+id/googleMap"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="vertical">



            <android.support.v7.widget.RecyclerView
                android:id="@+id/tag_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:descendantFocusability="blocksDescendants"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:layoutManager="LinearLayoutManager">

            </android.support.v7.widget.RecyclerView>

        </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/map_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical"
                    android:nestedScrollingEnabled="false">
                </android.support.v7.widget.RecyclerView>

            </LinearLayout>

        </LinearLayout>

</android.support.v4.widget.NestedScrollView>

这是我的适配器代码,它太长了,所以我只上传相关部分。当滚动到RecyclerView的底部时,它会添加一个新的项目。

public interface OnLoadMoreListener {
    void onLoadMore();
}

public MyAdapter(OnLoadMoreListener onLoadMoreListener) {
    this.onLoadMoreListener = onLoadMoreListener;
    mDataset = new ArrayList<>();
}

public void setLinearLayoutManager(LinearLayoutManager linearLayoutManager) {
    this.mLinearLayoutManager = linearLayoutManager;
}

public void setRecyclerView(final RecyclerView mView) {
    mView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
                visibleItemCount = recyclerView.getChildCount();
                totalItemCount = mLinearLayoutManager.getItemCount();
                firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
                lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();

                if (!isMoreLoading && (totalItemCount - visibleItemCount)<= (firstVisibleItem + visibleThreshold)) {
                    if (onLoadMoreListener != null) {
                        onLoadMoreListener.onLoadMore();
                        isMoreLoading = true;
                    }
                }
            }
        }
    });
}

@Override
public int getItemViewType(int position) {
    return mDataset.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == VIEW_ITEM) {
        context = parent.getContext();
        return new StudentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.home_contents, parent, false));
    } else {
        context = parent.getContext();
        return new ProgressViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress, parent, false));
    }
}

public void addAll(List<mainitem> lst) {
    mDataset.clear();
    mDataset.addAll(lst);
    notifyDataSetChanged();
}

public void addItemMore(List<mainitem> lst) {
    mDataset.addAll(lst);
    notifyItemRangeChanged(mDataset.size()-6, mDataset.size());
}


public void setMoreLoading(boolean isMoreLoading) {
    this.isMoreLoading=isMoreLoading;
}

@Override
public int getItemCount() {
    return mDataset.size();
}

public void setProgressMore(final boolean isProgress) {
    if (isProgress) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                mDataset.add(null);
                notifyItemInserted(mDataset.size() - 1);
            }
        });
    } else if(!isProgress) {
        mDataset.remove(mDataset.size() - 1);
        notifyItemRemoved(mDataset.size()-1);
    }
}
2个回答

1

使用android:nestedScrollingEnabled="false"来设置您的RecyclerView

<android.support.v7.widget.RecyclerView
    android:id="@+id/map_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:nestedScrollingEnabled="false"
    app:layoutManager="LinearLayoutManager"/>

2
我已经在我的Java代码中添加了它,但对我没有起作用。 - Jshin
它改变了我的RecyclerView的布局方式,就像LinearLayout一样,但还是感谢您的建议。 - Jshin
@Jshin 从 RecyclerView 中删除这个 app:layout_behavior="@string/appbar_scrolling_view_behavior",然后再试一下。 - AskNilesh
当第一个项目加载并到达回收视图底部时,它不会引发任何事件。您能为我的适配器代码提供建议吗? - Jshin
@Jshin,请查看此答案 https://dev59.com/vlYN5IYBdhLWcg3w9MNJ#46638845,了解如何使用嵌套滚动视图在Recyclerviw中加载更多。 - AskNilesh
显示剩余3条评论

0
上述解决方案仅适用于使用21以上版本的API,如果您想支持这个解决方案,同时也要支持低版本,请查看我的答案Stackoverflow Answer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接