【问题标题】:RecyclerView scrolling laggy on newer android versionsRecyclerView 在较新的 android 版本上滚动滞后
【发布时间】:2019-02-27 22:50:43
【问题描述】:

我的RecyclerView 在冷启动应用程序时开始滚动第一个项目时非常滞后。这种行为发生在最新的 android 版本(在 API 级别 27 和 28 上测试)而不是旧版本(在 API 级别 22 上测试)。

我尝试了两个版本,普通的com.android.support:appcompat-v7:28.0.0 和 androidx com.google.android.material:material:1.1.0-alpha03

测试项目非常简单:

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RecyclerView recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    recyclerView.setItemViewCacheSize(20);

    ArrayList<String> list = new ArrayList<>();

    for (int i = 0; i < 200; i++) {
        list.add(String.valueOf(i));
    }

    MyAdapter adapter = new MyAdapter(MainActivity.this, list);
    recyclerView.setAdapter(adapter);
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

List<String> mData;
Context context;
LayoutInflater mInflater;

public MyAdapter(Context context, List<String> data) {
    this.mData = data;
    this.context = context;
    this.mInflater = LayoutInflater.from(context);
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = mInflater.inflate(R.layout.recycler_item, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    viewHolder.tv.setText(String.valueOf(i));
}

@Override
public int getItemCount() {
    return mData.size();
}

public String getItem(int id) {
    return mData.get(id);
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView tv;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        tv = itemView.findViewById(R.id.item_tv);
    }
}
}

recycler_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">

<TextView
    android:id="@+id/item_tv"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

有解决问题的想法还是我自己无法解决的库问题(等待新的正式版本)?

【问题讨论】:

  • 您是否尝试过删除 setItemViewCacheSize 并注意到不同之处?
  • 是的,我做到了(也是 setHasFixedSize(true)),但没有区别。在 GPU Profiler 中,条形图到达屏幕顶部(几乎是绿色),稍后,当它“加载”时,它会保持在 16 毫秒的区域内。
  • 使用ConstraintLayout时有什么变化吗?

标签: android android-recyclerview android-support-library lag


【解决方案1】:

在您的项目布局中,包装类是 ConstraintLayoutwrap_content_height - 它可能会在测量时导致问题。

  • 因为你有一个单一的内部项目,你可以摆脱包装,只保留一个 TextView 作为根元素
  • 如果您仍想为视图获取包装器 - 尽量避免为 RelativeLayout 和/或 ConstrainLayout 使用 wrap_content,请使用固定大小或简单布局,例如 Frame/Linear。

【讨论】:

    【解决方案2】:

    从父上下文中获取layoutInflater而不是

    mInflater.inflate(R.layout.recycler_item, viewGroup, false);
    

    使用这个

    LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, viewGroup,
                        false);
    

    希望这会有所帮助

    【讨论】:

      【解决方案3】:

      在找到此评论后,我通过将构建变体设置为发布解决了该问题:Performance of ConstraintLayout inside RecyclerView ViewHolder

      这里分享的例子很流畅,我的 Glide 应用程序也执行得更流畅,即使没有将 layout_width 设置为 ConstraintLayout。

      但我不清楚为什么这种延迟出现在调试模式下,并且只出现在最新的 android 版本上。

      【讨论】:

        猜你喜欢
        • 2023-04-06
        • 2018-01-30
        • 1970-01-01
        • 1970-01-01
        • 2014-10-08
        • 2017-06-14
        • 1970-01-01
        • 2021-08-14
        相关资源
        最近更新 更多