Android 在NestedScrollView中使用RecyclerView时,不会回收视图
我正在使用Android 在NestedScrollView中使用RecyclerView时,不会回收视图,android,android-recyclerview,nestedscrollview,recycle,Android,Android Recyclerview,Nestedscrollview,Recycle,我正在使用RecyclerView内部NestedScrollView。此外,我还将setNestedScrollingEnabled设置为false,用于recyclerview 支持较低的API ViewCompat.setNestedScrollingEnabled(mRecyclerView,false) 现在!当用户滚动视图时,一切似乎都正常,但是!!!recyclerview中的视图不可回收!!!堆的大小迅速增长 更新: RecyclerView布局管理器是交错布局管理器 fragm
RecyclerView
内部NestedScrollView
。此外,我还将setNestedScrollingEnabled
设置为false,用于recyclerview
支持较低的API
ViewCompat.setNestedScrollingEnabled(mRecyclerView,false)代码>
现在!当用户滚动视图时,一切似乎都正常,但是!!!recyclerview中的视图不可回收!!!堆的大小迅速增长
更新:
RecyclerView布局管理器是交错布局管理器
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>
<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>
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();
}
}
});
注意:我已经将load more设置为scroll view,因为recyclerview
可以连续执行,并且不可停止
感谢您提供的任何帮助这是因为我们有一个在滚动视图中具有滚动行为的回收器视图。(在卷轴内滚动)
我认为解决此问题的最佳方法是将您的profileCardview作为回收器视图的标题,然后删除嵌套的滚动视图
如果它是一个listview,那么它与listview.addHeaderView(profileCardView)一样简单,但是对于Recycler视图,没有addheadview的等价物。因此,您可以参考下面的链接来更改您的实现
对于RecyclerView或ListView,高度应该是恒定的,因为如果它的大小不是恒定的,那么它将如何管理内存中最大可见行数。尝试更改RecyclerView属性android:layout\u height=“match\u parent”
或固定高度(如需要时“300dp”
),而不是“wrap\u content”
。它可以改善您的内存管理。您能解释一下为什么在NestedScrollView
中添加RecyclerView
吗?@rom4ek@MAY3AM更新您的代码和xmlNestedScrollView
绘制所有子元素,因此,如果在NestedScrollView
中使用了RecyclerView
,适配器的所有元素都将在第一时间加载。更好的方法是仅使用RecyclerView
并使用getItmeViewType
在适配器中返回cardView
如果(位置==0)
。正如热汉所建议的,我通常使用@Pr38y。您面临的性能问题是什么?@CliveJefferies将布局高度设置为固定大小将在更新子视图时减少重新绘制。您的意思是“不要担心,如果我的子视图因其大小固定而发生更改,则无需重新绘制此视图”。从理论上讲,你可以说它可能会减少内存占用,但主要是它将消除UI阻塞垃圾收集,这将使你的应用程序更加平滑。