0
\$\begingroup\$

My app, particularly the MainActivity suffers from some lag when I scroll through the RecyclerView. I tried to fix OverDraw as much as I can.

This is what my OverDraw screenshot looks like:

OverDraw

activity_main.xml:

<?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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ChordsList.ChordsListActivity">

<RelativeLayout
    android:id="@+id/layout_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:scaleType="fitStart"
        android:src="@drawable/icon_verde_acqua_tras"/>

    <ImageButton
        android:id="@+id/menu"
        android:background="@color/trans"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/tasto_menu"
        android:scaleType="fitCenter"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="20dp"
        android:onClick="showMenu"/>

    <ImageButton
        android:id="@+id/search"
        android:background="@color/trans"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/tasto_ricerca"
        android:scaleType="fitCenter"
        android:layout_centerVertical="true"
        android:onClick="onSearchButtonPressed"
        android:layout_toStartOf="@id/menu"
        android:layout_marginTop="20dp" />

    <ImageButton
        android:id="@+id/tuner"
        android:background="@color/trans"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:onClick="onTunerButtonPressed"
        android:src="@drawable/tuner"
        android:scaleType="fitCenter"
        android:layout_toStartOf="@id/search"
        android:layout_marginTop="20dp" />



</RelativeLayout>

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chords_recycler"
    android:layout_below="@+id/layout_top"
    android:layout_alignParentStart="true"
    android:layout_marginTop="25dp" />
</RelativeLayout>

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">

<TextView
    android:id="@+id/nome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAllCaps="true"
    android:layout_centerVertical="true"
    android:textSize="25sp"
    android:layout_marginTop="10dp"
    android:fontFamily="sans-serif-condensed"
    android:textStyle="bold"
    android:textColor="@color/black" />

<ImageView
    android:id="@+id/immagine_accordo"
    android:layout_width="130dp"
    android:layout_marginTop="5dp"
    android:layout_height="100dp"
    android:scaleType="fitCenter"
    android:layout_alignParentEnd="true"/>

</RelativeLayout>

Assuming that the problem is OverDraw (please tell me if it's not), how can I improve it further? I need everything that is in the Image, so I cannot remove the background.

As suggested by Ivanhoe, OverDraw does not seem to be a problem. The Adapter class and binding could be.

Adapter class:

public class ChordsListAdapter extends RecyclerView.Adapter<ChordsListAdapter.MyViewHolder> {
private ArrayList<Accordo> chordsList;
Context c;



public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView nome; //note
    public ImageView immagine;

    public MyViewHolder(View view) {
        super(view);
        nome = (TextView) view.findViewById(R.id.nome);
        immagine = (ImageView) view.findViewById(R.id.immagine_accordo);
    }
}

public ChordsListAdapter(Context c, ArrayList<Accordo> chordsList) {
    this.chordsList = chordsList;
    this.c = c;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Accordo chord = chordsList.get(position);
    holder.nome.setText(chord.getName());
    holder.immagine.setImageResource(chord.getImage()[0]);
}

@Override
public int getItemCount() {
    return chordsList.size();
}
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Taking for granted that the problem is OverDraw How do you know? \$\endgroup\$
    – Mast
    Oct 1, 2016 at 14:49
  • \$\begingroup\$ I don't know, that's why I added "please tell me if it's not" :) \$\endgroup\$
    – Daniele
    Oct 1, 2016 at 14:50
  • \$\begingroup\$ I don't see any red in your overdraw snapshot so that's not why your list is laggy. \$\endgroup\$
    – Ivanhoe
    Oct 10, 2016 at 11:55
  • \$\begingroup\$ Could you provide your adapter's implementation? I suspect the problem might be in your "binding" code. \$\endgroup\$
    – Ivanhoe
    Oct 10, 2016 at 12:02
  • \$\begingroup\$ @Ivanhoe I have updated my question with the adapter class \$\endgroup\$
    – Daniele
    Oct 10, 2016 at 12:12

1 Answer 1

1
\$\begingroup\$

You're instantiating MyViewHolder for every binding, that means you're executing findViewById for each item which is an expensive method. A solution would be to cache the created instance of MyViewHolder in onCreateViewHolder by assigning it to the view tag, and reuse it. Have a look a this https://developer.android.com/training/improving-layouts/smooth-scrolling.html

\$\endgroup\$
1
  • \$\begingroup\$ This is true for a regular ListView implementation. The OP is using RecyclerView where this overhead is already taken care of. onCreateViewHolder() will only be called when it is needed developer.android.com/reference/android/support/v7/widget/… android.view.ViewGroup, int \$\endgroup\$
    – Ivanhoe
    Oct 10, 2016 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.