gpt4 book ai didi

android - RecyclerView 在 NestedScrollView 中使用时不会回收 View

转载 作者:IT王子 更新时间:2023-10-28 23:40:38 26 4
gpt4 key购买 nike

我在 NestedScrollView 中使用 RecyclerView。我还为 recyclerview

setNestedScrollingEnabled 设置为 false

支持较低的API

ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);

现在!当用户 ScrollView 时,一切似乎都很好,但是!!! recyclerview 中的 View 不会被回收!!!并且堆大小迅速增长!

更新:RecyclerView 布局管理器是 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();
}

}

});

P.s : 我已经为 ScrollView 设置了更多负载,因为 recyclerview 会连续执行并且无法停止!

感谢任何帮助

最佳答案

这是因为我们有一个回收器 View ,它在 ScrollView 中具有滚动行为。 (在卷轴内滚动)

我认为解决此问题的最佳方法是将您的 profileCardview 作为回收站 View 中的标题,然后删除嵌套的 ScrollView 。

如果它是一个 ListView ,那么它就像 listView.addHeaderView(profileCardView) 一样简单,但是对于 Recycler View ,没有 addheadview 等效项。因此,您可以引用以下链接来更改您的实现。

Is there an addHeaderView equivalent for RecyclerView?

关于android - RecyclerView 在 NestedScrollView 中使用时不会回收 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37590651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com