RecyclerView需要很长时间才能加载

时间:2016-05-25 09:20:41

标签: android android-recyclerview

我正在为500件商品创建回收商视图。它为每个项目调用onCreateViewHolder和onBindViewHolder并且加载时间太长。由于这个原因,用户界面跳过大约522帧。如何让它加载更快?

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;
    }

我不明白为什么加载这么长时间。

2 个答案:

答案 0 :(得分:11)

我在RecyclerView内使用了ScrollView,导致它像蜗牛一样加载。刚删除了滚动视图,一切都像魅力一样。

答案 1 :(得分:1)

Imho你有几个选择:

  • 对列表进行分页 - 例如加载前50个项目,当用户到达底部时,再加载50个......(如建议的here
  • 从空列表和进度条开始。然后从后台线程填充数组(例如逐步)并在每次更新后调用notifyDataSetChanged()

如果数据集是固定/已知的(如你的情况下有一个歌曲列表),第二个选项可能是可行的。