[英]RecyclerView scrolling laggy on newer android versions
My RecyclerView
is pretty laggy whenever I start to scroll over the first items in a cold app start. 每当我开始在冷启动的应用程序中滚动浏览第一个项目时,我的
RecyclerView
就会显得很迟钝。 This behavior happens for the latest android versions (tested on API level 27 and 28) but not older ones (tested on API level 22). 对于最新的android版本(在API级别27和28上进行测试),会发生这种情况,而在较早的android版本(在API级别22上进行测试)不会发生这种情况。
I tried both versions, the normal com.android.support:appcompat-v7:28.0.0
and androidx com.google.android.material:material:1.1.0-alpha03
. 我尝试了两个版本,正常的
com.android.support:appcompat-v7:28.0.0
和androidx com.google.android.material:material:1.1.0-alpha03
。
The test project is pretty simple: 测试项目非常简单:
MainActivity.java 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 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 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 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>
Any ideas for solving the problem or is it a library issue which can't be solved by myself (waiting for new official release)? 有任何解决问题的想法,还是我自己无法解决的图书馆问题(等待新的正式发布)?
get layoutInflater
from parent context so instead of 从父上下文获取
layoutInflater
,而不是
mInflater.inflate(R.layout.recycler_item, viewGroup, false);
use this 用这个
LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, viewGroup,
false);
Hope this will help 希望这会有所帮助
In you item layout the wrapper class is ConstraintLayout
with wrap_content_height
- it could cause problems while measuring. 在项目布局中,包装器类是具有
wrap_content_height
ConstraintLayout
它可能会在测量时引起问题。
RelativeLayout
and/or ConstrainLayout
, use fix size or simple layouts such as Frame/Linear. RelativeLayout
和/或ConstrainLayout
使用wrap_content,请使用修订大小或简单的布局,例如Frame / Linear。 I solved the problem by setting the build variant to release after finding this comment: Performance of ConstraintLayout inside RecyclerView ViewHolder . 找到以下注释后,我通过将构建变体设置为release来解决了该问题: RecyclerView ViewHolder中ConstraintLayout的性能 。
The example shared here is smoothly then and my application with Glide performs more smoothly, too, even without setting layout_width to ConstraintLayout. 此处共享的示例很流畅,即使没有将layout_width设置为ConstraintLayout,我使用Glide的应用程序也可以更流畅地运行。
But it's unclear to me why this lag appears in the debug mode and only on the latest android versions. 但是我不清楚为什么这个滞后会出现在调试模式下并且仅出现在最新的android版本上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.