我正在为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;
}
我不明白为什么加载这么长时间。
答案 0 :(得分:11)
我在RecyclerView
内使用了ScrollView
,导致它像蜗牛一样加载。刚删除了滚动视图,一切都像魅力一样。
答案 1 :(得分:1)
Imho你有几个选择:
notifyDataSetChanged()
。如果数据集是固定/已知的(如你的情况下有一个歌曲列表),第二个选项可能是可行的。