RecyclerView не перерабатывает представления, когда использует его внутри NestedScrollView
Я использую RecyclerView
внутри NestedScrollView
. Также я установил setNestedScrollingEnabled
в значение false для RecyclerView
для поддержки более низкого API
ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);
Теперь! Когда пользователь прокручивает представление, все кажется хорошо, но!!! просмотров в recyclerview не перерабатывается!!! и размер кучи быстро растет!
Обновление:
Менеджер компоновки 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: Я установил нагрузку больше для прокрутки, потому что RecyclerView
делает это непрерывно, и никто не останавливается!
Любая помощь приветствуется
Ответы
Ответ 1
Это связано с тем, что у нас есть просмотр recycler, который имеет поведение прокрутки внутри прокрутки. (прокрутка внутри прокрутки)
Я думаю, что лучший способ решить эту проблему - это ваш profileCardview как заголовок в представлении recycler, а затем удалить вложенное представление прокрутки.
Если это был список, то это было так же просто, как listView.addHeaderView(profileCardView), но для просмотра Recycler нет эквивалента addheadview. Следовательно, вы можете ссылаться на приведенную ниже ссылку, чтобы изменить вашу реализацию.
Есть ли эквивалент addHeaderView для RecyclerView?
Ответ 2
Для RecyclerView или ListView высота должна быть постоянной, потому что, если она не будет иметь постоянный размер, то как она будет управлять максимальным количеством видимых строк в памяти. Попробуйте изменить атрибут RecyclerView android:layout_height="match_parent"
вместо "wrap_content"
. Это должно улучшить управление памятью.