我的应用程序的MainActivity类由ViewPager和大约10个选项卡组成。在每个选项卡中,我使用android RecyclerView加载ListItem。用户在ViewPager中左右滑动时会出现此问题。调试后,我发现我的CustomListAdapter中出现了问题。
我发现如果我在onCreateViewHolder中增加Layout Inflater的数量,滞后会增加。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
// performance hit
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row, parent, false);
View itemViewlast = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row_last, parent, false);
View itemViewdefault = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row_default, parent, false);
switch (viewType) {
case 0:
viewHolder = new MyViewHolder(itemView);
break;
case 3:
viewHolder = new MyViewHolder(itemViewlast);
break;
default:
viewHolder = new MyViewHolder(itemView1);
break;
}
return viewHolder;
}
为了减少我在开关盒本身内部移动了LayoutInflater。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
switch (viewType) {
case 0:
// moved here
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row, parent, false);
viewHolder = new MyViewHolder(itemView);
break;
case 3:
// moved here
View itemViewlast = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row_last, parent, false);
viewHolder = new MyViewHolder(itemViewlast);
break;
default:
// moved here
View itemViewdefault = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row_default, parent, false);
viewHolder = new MyViewHolder(itemView1);
break;
}
return viewHolder;
}
现在我还是觉得还有一些滞后。所以我的问题是,有没有办法减少这种滞后或在onCreateViewHolder方法之外移动Layout Inflater?
MyViewHolder方法:
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, rating, genre,detailmovie,movieName,movieTime;
public ImageView thumbNail, movieNail;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
rating = (TextView) view.findViewById(R.id.rating);
genre = (TextView) view.findViewById(R.id.genre);
detailmovie = (TextView) view.findViewById(R.id.detailmovie);
movieName = (TextView) view.findViewById(R.id.movieName);
movieTime = (TextView) view.findViewById(R.id.movieTime);
thumbNail = (ImageView) view.findViewById(R.id.thumbnail);
movieNail = (ImageView) view.findViewById(R.id.movieThumbnail);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case 0:
MyViewHolder vh1 = (MyViewHolder) viewHolder;
configureViewHolder1(vh1, position);
break;
case 3:
MyViewHolder vh3 = (MyViewHolder) viewHolder;
configureViewHolder1(vh3, position);
break;
default:
MyViewHolder vh4 = (MyViewHolder) viewHolder;
configureViewHolder1(vh4, position);
break;
}
}
private void configureViewHolder1(MyViewHolder vh1, int position) {
final Movie movie = movieItems.get(position);
vh1.title.setText(movie.getTitle());
vh1.rating.setText("Rating: " + String.valueOf(movie.getRating()));
Glide.with(activity).
load(Uri.parse(movie.getThumbnailUrl())).
crossFade().
placeholder(R.drawable.placeholder).diskCacheStrategy(DiskCacheStrategy.SOURCE).
dontTransform().
into(vh1.thumbNail);
vh1.movieDetail.setText(String.valueOf(movie.movieDetail()));
vh1.movieName.setText(String.valueOf(movie.getmovieName()));
vh1.movieTime.setText(String.valueOf(movie.getmovieTime()));
vh1.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragmentJump(movie);
}
});
}
答案 0 :(得分:0)
尝试使用默认的caching strategy for glide(DiskCacheStrategy.RESULT
),它将生成的图像存储在缓存中,而不是存储完整的图像。
通过这样做,您可以减少对图像的变换量(减少处理次数 - >减少丢帧次数的机会)。