У меня есть NestedScrollView, который содержит горизонтальный адаптер и вертикальный адаптер.Когда я устанавливаю адаптер или обновляю адаптер, он всегда временно замирает (несколько секунд - с профилем это занимает больше времени) и ухудшается, когда добавляется больше элементов!
Вот загрузка процессора, но я не понимаю, почему это происходит, что я могу сделать лучше.
Мой код для адаптера вполне нормален:
adapter = new Adapter(data);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapter);
Любая идея, что может быть не так или как улучшить эту иерархию?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white_01">
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottomMenu"
android:orientation="horizontal">
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/white_01"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_05"
android:orientation="vertical">
<TextView
android:id="@+id/textBestSeller"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:textColor="@color/gray_01"
android:textSize="18sp"
android:textStyle="bold"
tools:text="Nejprodávanější" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rc1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white_01"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/cardOrder"
layout="@layout/shared_card_two_lines"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<include
android:id="@+id/cardFilter"
layout="@layout/shared_card_two_lines"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Nalezeno 325 produktů"
android:textSize="12sp"
tools:text="Nalezeno 325 produktů" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Zrušit všechny filtry"
android:textAllCaps="true"
android:textColor="@color/blue_01"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rc2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<include
layout="@layout/loading_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
tools:visibility="visible" />
<include
layout="@layout/retry_layout"
android:visibility="gone"
tools:visibility="gone" />
<include
android:id="@+id/bottomMenu"
layout="@layout/shared_bottom_menu"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_menu_height"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Примечание 1: Первый линейный макет имеет причину, это «общий» вид, который содержит содержимое.
Примечание 2: Я пытаюсь добиться такого же поведения, как GearBest на домашней странице (прокрутите вниз).Для них это, кажется, работает хорошо, так что это выполнимо.
Адаптер, который все еще зависает:
public class SomeAdapter extends RecyclerView.Adapter<SomeAdapter.ViewHolder> {
private List<ProductItem> items;
public SomeAdapter(List<ProductItem> items) {
this.items = items;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
}
@Override
public int getItemCount() {
return items.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View itemView) {
super(itemView);
}
}
}
ОБНОВЛЕНИЕ: После упрощения фрагмента к этому проблема все еще существует.Кажется, что по какой-то причине комбинация раздутого макета и RecyclerView внутри NestedScrollView вызывает зависание.Я продолжаю поиск.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>