Search code examples
androidandroid-recyclerviewandroid-support-librarylag

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. This behavior happens for the latest android versions (tested on API level 27 and 28) but not older ones (tested on API level 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.

The test project is pretty simple:

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>

Any ideas for solving the problem or is it a library issue which can't be solved by myself (waiting for new official release)?


Solution

  • I solved the problem by setting the build variant to release after finding this comment: Performance of ConstraintLayout inside RecyclerView ViewHolder.

    The example shared here is smoothly then and my application with Glide performs more smoothly, too, even without setting layout_width to ConstraintLayout.

    But it's unclear to me why this lag appears in the debug mode and only on the latest android versions.