![](/img/trans.png)
[英]Set onItemClickListener inside onBindViewHolder() with RecyclerView.Adapter
[英]Why set/update RecyclerView.Adapter inside NestedScrollView temporary freezing?
我有NestedScrollView,其中包含水平适配器和垂直适配器。 当我设置适配器或更新适配器时,它总是临时冻结(几秒钟-使用配置文件需要更长的时间),并且当添加更多项时,情况会变得更糟!
这是CPU负载,但是我不知道为什么会发生,我可以做得更好。
我的适配器代码很正常:
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);
}
}
}
更新:对此简化片段后,问题仍然存在。 似乎出于某种原因,NestedScrollView中膨胀的布局和RecyclerView的组合会导致冻结。 我继续搜寻。
<?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>
您的布局层次结构很深,正在查看适配器,该适配器什么也不做,可能需要很长时间,因为它必须测量所有内容。 参见https://developer.android.com/topic/performance/vitals/render#common-jank
该页面还告诉您如何记录和调试问题。
从您使用许多 LinearLayouts表示的相当简单的布局来看,我认为使用ConstraintLayout
可以做得更好,还请参见ConstraintLayout的用法 。 如解决方案中所述 。
再看一下您的布局,我看到一个RelativeLayout
一个NeatedScrollView
和LinearLayout
(从布局根目录看到)似乎NeatedScrollView
。 我建议您删除它们。 (这是根据您的屏幕截图判断的,因为您提供的布局具有不同的结构。)
经过一些研究,唯一可行的解决方案是删除NestedScrollView,并将所有内容放入一个回收器视图中,并在适配器内部将它们作为不同类型的视图支架进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.