1

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)?

Vkay
  • 495
  • 1
  • 6
  • 21
  • Have you tried removing setItemViewCacheSize and noticed a difference? – Ivan Wooll Feb 27 '19 at 12:08
  • Yes, I did (also setHasFixedSize(true)) but no difference. In the GPU Profiler the bars are reaching the top of the screen (almost green), later, when it's "loaded" it keeps in 16ms area. – Vkay Feb 27 '19 at 12:13
  • 1
    any changes when _not_ using ConstraintLayout? – Zun Feb 27 '19 at 12:45

4 Answers4

4

In you item layout the wrapper class is ConstraintLayout with wrap_content_height - it could cause problems while measuring.

  • Since you have a single itme inside you could just get rid of wrapper and keep just single TextView as a root element
  • If you still wants to get wrapper for the view - try to avoid wrap_content for RelativeLayout and/or ConstrainLayout, use fix size or simple layouts such as Frame/Linear.
dilix
  • 3,761
  • 3
  • 31
  • 55
2

get layoutInflaterfrom parent context so instead of

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

Moorthy
  • 723
  • 1
  • 6
  • 20
2

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.

Vkay
  • 495
  • 1
  • 6
  • 21
2

I was facing same problem and solved by using recyclerview within nested scroll view. like below code:

    <androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarSize="5dp"
    android:scrollbars="vertical"
    android:layout_above="@id/ListBannerAds">
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:scrollbarSize="5dp"
        android:nestedScrollingEnabled="false"
        android:id="@+id/ZaboorListRecyclerview"/>
</androidx.core.widget.NestedScrollView>
  • 3
    Man, Thank you so much! I've been rewriting adapters since 3 days and nothing worked until I saw this solution. Thank you from all my heart and hate android development from all my heart too. – MBH Jan 26 '23 at 03:51