This Activity has got a RecyclerView
that is inflated with an ArrayList
that is ca. 400 Elements.
This is what an Element of the ArrayList looks like:
chords.add(new Accordo(new int[]{
R.drawable.do_maggiore, R.drawable.do_maggiore_alt1, R.drawable.do_maggiore_alt2,
R.drawable.do_maggiore_alt3, R.drawable.do_maggiore_alt4, R.drawable.do_maggiore_alt5,
R.drawable.do_maggiore_alt6}, R.string.do_maggiore, "Do, Mi, Sol", R.raw.do_maggiore));
So it has got some images, two Strings and a Sound file.
This whole List is inflated into the RecyclerView at Activity's launch. This causes the Activity to have some lag when I scroll fast through the List.
How can I improve the performance of this class?
code:
public class ChordsListActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
ArrayList<Accordo> chords;
ImageButton cerca;
RecyclerView recyclerView;
InterstitialAd chordsListAd;
boolean isPremium;
static final String PREFERENCES = "prefs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chords_list);
SharedPreferences settings = getSharedPreferences(PREFERENCES,0);
isPremium = settings.getBoolean("status", false);
/** gets the list */
chords = new ArrayList<>();
ChordsList list = new ChordsList();
list.createList();
chords = list.getList();
/** inflates the list into the recyclerview */
recyclerView = (RecyclerView) findViewById(R.id.chords_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(ChordsListActivity.this, LinearLayoutManager.VERTICAL, false));
recyclerView.setHasFixedSize(true);
final ChordsListAdapter adapter = new ChordsListAdapter (this, chords);
recyclerView.setAdapter(adapter);
/** manages ads */
if (!isPremium) {
MobileAds.initialize(getApplicationContext(), "ca-app-pub-6723047396589178/2654753246");
AdView listBanner = (AdView) findViewById(R.id.chords_list_banner);
AdRequest adRequest = new AdRequest.Builder().build();
listBanner.loadAd(adRequest);
/** loads full-screen add */
chordsListAd = new InterstitialAd(this);
chordsListAd.setAdUnitId("ca-app-pub-6723047396589178/7447672046");
requestNewInterstitial();
chordsListAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
}
});}
/** manages what happens when a recyclerview item is clicked */
ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener(){
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
if (!isPremium) {
if (chordsListAd.isLoaded())
chordsListAd.show();
}
Intent intent = new Intent(ChordsListActivity.this, ChordActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("selected", chords.get(position));
intent.putExtras(bundle);
startActivity(intent);
}
});
/** reads show_dialog preferences and if false opens the PopUpInfoActivity */
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean showDialog = sharedPref.getBoolean("Show_dialog", true);
Intent legendaIntent = new Intent(ChordsListActivity.this, PopUpInfoActivity.class);
if(showDialog)
startActivity(legendaIntent);
/** manages what happens when search button is pressed */
cerca = (ImageButton) findViewById(R.id.search);
cerca.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ChordsListActivity.this, SearchActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("chords", chords);
intent.putExtras(bundle);
startActivity(intent);
}
});}
/** queries server for new interstitial add */
private void requestNewInterstitial() {
AdRequest request = new AdRequest.Builder().build();
chordsListAd.loadAd(request);
}
/** manages what happens in the menu */
public void showMenu(View v) {
PopupMenu menu = new PopupMenu(this, v);
menu.setOnMenuItemClickListener(this);
menu.inflate(R.menu.menu);
menu.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.informazioni_menu:
Intent intent = new Intent(ChordsListActivity.this, Info.class);
startActivity(intent);
return true;
case R.id.legenda_menu:
Intent legendaIntent = new Intent(ChordsListActivity.this, PopUpInfoActivity.class);
startActivity(legendaIntent);
return true;
case R.id.premium_menu:
Intent premiumIntent = new Intent(ChordsListActivity.this, Premium.class);
startActivity(premiumIntent);
return true;
case R.id.problema_menu:
Intent email = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "[email protected]", null));
email.putExtra(Intent.EXTRA_SUBJECT, "Chords Problem");
email.putExtra(Intent.EXTRA_TEXT, true);
startActivity(Intent.createChooser(email, getResources().getString(R.string.choose_client)));
return true;
case R.id.invita:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.invito_oggetto));
shareIntent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.invita_messaggio) +
" https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName());
startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.condividi_con)));
default:
return false;
}
}
}
Here is my ChordsListAdapter
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);
//note = (TextView) view.findViewById(R.id.note);
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.note.setText(chord.getNote());
holder.immagine.setImageResource(chord.getImage()[0]);
}
@Override
public int getItemCount() {
return chordsList.size();
}
}
Here is my model Accordo.class
(Accordo means Chord)
public class Accordo implements Parcelable {
int[] image;
int name;
String note;
int sound;
public Accordo(int[] image, int name, String note, int sound) {
this.image = image;
this.name = name;
this.note = note;
this.sound = sound;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public int[] getImage() {
return image;
}
public void setImage(int[] image) {
this.image = image;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public int getSound() { return sound; }
public void setSound(int sound) { this.sound = sound; }
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeIntArray(this.image);
dest.writeInt(this.name);
dest.writeString(this.note);
dest.writeInt(this.sound);
}
protected Accordo(Parcel in) {
this.image = in.createIntArray();
this.name = in.readInt();
this.note = in.readString();
this.sound = in.readInt();
}
public static final Creator<Accordo> CREATOR = new Creator<Accordo>() {
@Override
public Accordo createFromParcel(Parcel source) {
return new Accordo(source);
}
@Override
public Accordo[] newArray(int size) {
return new Accordo[size];
}
};
}
Here is my chords_list_activity.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="50dp"
android:layout_height="50dp"
android:src="@drawable/tasto_menu"
android:scaleType="fitCenter"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"
android:onClick="showMenu"/>
<ImageButton
android:id="@+id/search"
android:background="@color/trans"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/tasto_ricerca"
android:scaleType="fitCenter"
android:layout_toStartOf="@id/menu"
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" />
<com.google.android.gms.ads.AdView
android:id="@+id/chords_list_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId='@string/chords_list_banner'>
</com.google.android.gms.ads.AdView>
</RelativeLayout>
Please provide suggestions by which I can increase the performance of my App
Thanks a lot in advance!!