I'm creating a recycler view for 500 items. It is calling onCreateViewHolder and onBindViewHolder for every item and taking too long to load. The UI skipped around 522 frames because of this. How can I make it load faster?
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder.getItemViewType() == SHUFFLE_ROW) {
Log.d(TAG, "Position " + position);
if(searchClicked) {
final SearchBoxHolder searchHolder = (SearchBoxHolder) holder;
searchHolder.backIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}
else {
ShuffleAllViewHolder shuffleHolder = (ShuffleAllViewHolder) holder;
shuffleHolder.numOfSongs.setText(mSongList.size() + " Songs");
}
}
else if(holder.getItemViewType() == SONG_ROW) {
ListViewHolder listViewHolder = (ListViewHolder) holder;
Song currentSong = mSongList.get(position-1);
listViewHolder.albumTitle.setText(currentSong.getTitle());
listViewHolder.albumArtist.setText(currentSong.getArtist());
Picasso.with(mContext).load(currentSong.getAlbumArt()).placeholder(R.drawable.placeholder)
.resize(70, 70)
.centerCrop()
.into(listViewHolder.albumImage);
}
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == SHUFFLE_ROW) {
if(searchClicked) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_row, parent, false);
return new SearchBoxHolder(view);
}
else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shuffle_all_row, parent, false);
return new ShuffleAllViewHolder(view);
}
}
else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.song_row, parent,
false);
return new ListViewHolder(view);
}
public int getItemViewType(int position) {
if(position == 0) {
return SHUFFLE_ROW;
}
else return SONG_ROW;
}
I don't understand why it is taking so long to load.
I was using RecyclerView
inside ScrollView
which was causing it to load like a snail. Just removed the scrollview and everything works like a charm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With