RecyclerView在NestedScrollView中使用时不会回收视图

我在NestedScrollView内使用RecyclerView 。 另外我设置setNestedScrollingEnabled为false为recyclerview

支持较低的API

ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);

现在! 当用户滚动视图的每件事情似乎没关系,但! recyclerview中的视图不会被回收! 堆大小迅速增长!

更新 :RecyclerView布局pipe理器是StaggeredLayoutManager

fragment_profile.xml

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/coordinator" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" > </android.support.design.widget.AppBarLayout> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/profileSwipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- RecyclerView and NestedScrollView --> <include layout="@layout/fragment_profile_details" /> </android.support.v4.widget.SwipeRefreshLayout> </android.support.design.widget.CoordinatorLayout> 

fragment_profile_details.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rootLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:orientation="vertical" > <android.support.v4.widget.NestedScrollView android:id="@+id/nested_scrollbar" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="fill_vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:fillViewport="true" android:scrollbars="none" > <LinearLayout android:id="@+id/nested_scrollbar_linear" android:layout_width="match_parent" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" android:orientation="vertical" > <android.support.v7.widget.CardView android:id="@+id/profileCardview" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardBackgroundColor="@color/card_backgroind" app:cardCornerRadius="0dp" app:cardElevation="0dp" > <!-- Profile related stuff like avatar and etc. ---> </android.support.v7.widget.CardView> <android.support.v7.widget.RecyclerView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/four" android:layout_marginEnd="@dimen/four" android:layout_marginLeft="@dimen/four" android:layout_marginRight="@dimen/four" android:layout_marginStart="@dimen/four" android:layout_marginTop="@dimen/four" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:clipToPadding="false" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> </LinearLayout> 

ProfileFragment.java

 mAdapter = new MainAdapter(getActivity(), glide, Data); listView = (RecyclerView) view.findViewById(R.id.list_view); ViewCompat.setNestedScrollingEnabled(listView, false); listView.setAdapter(mAdapter); mStaggeredLM = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); mStaggeredLM.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); listView.setLayoutManager(mStaggeredLM); mScroll.setOnScrollChangeListener(new OnScrollChangeListener() { @Override public void onScrollChange(NestedScrollView arg0, int arg1, int arg2, int arg3, int arg4) { View view = (View) mScroll.getChildAt(mScroll.getChildCount() - 1); int diff = (view.getBottom() - ( mScroll.getHeight() + mScroll.getScrollY())); if(diff == 0){ int visibleItemCount = mStaggeredLM.getChildCount(); int totalItemCount = mStaggeredLM.getItemCount(); int[] lastVisibleItemPositions = mStaggeredLM.findLastVisibleItemPositions(null); int lastVisibleItemPos = getLastVisibleItem(lastVisibleItemPositions); Log.e("getChildCount", String.valueOf(visibleItemCount)); Log.e("getItemCount", String.valueOf(totalItemCount)); Log.e("lastVisibleItemPos", String.valueOf(lastVisibleItemPos)); if ((visibleItemCount + 5) >= totalItemCount) { mLoadMore.setVisibility(View.VISIBLE); Log.e("LOG", "Last Item Reached!"); } mMore = true; mFresh = false; mRefresh = false; getPosts(); } } }); 

Ps:我已经设置了更多的滚动视图,因为recyclerview连续不断地执行并且没有停止!

任何帮助表示赞赏

这是因为我们有一个回滚视图,它在滚动视图内具有滚动行为。 (在卷轴内滚动)

我认为解决这个问题的最好方法就是将你的profileCardview作为你的回收站视图中的标题,然后删除嵌套的滚动视图。

如果它是一个列表视图,那么它就像listView.addHeaderView(profileCardView)一样简单,但对于Recycler视图没有addheadview等效。 因此你可以参考下面的链接来改变你的实现。

RecyclerView有一个addHeaderView等价物吗?

对于RecyclerView或ListView,高度应该是不变的,因为如果它不是一个常量大小,那么它将如何pipe理内存中可见行的最大数量。 尝试通过更改RecyclerView属性android:layout_height="match_parent"而不是"wrap_content" 。 它应该改善你的memory management。