Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView taking too long to load

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.

like image 975
crysis Avatar asked May 25 '16 09:05

crysis


1 Answers

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.

like image 90
crysis Avatar answered Oct 12 '22 23:10

crysis