recyclerView列表大小为16.在我的手机上,第一次在屏幕上显示9个半单元格,这意味着调用了OnCreateViewHolder
方法的10倍。
之后如果我滚动,则调用OnCreateViewHolder
方法4次;这意味着它不会重复使用免费单元格。对于最后两个单元格,不调用OnCreateViewHolder
;这意味着它现在正在重复使用该单元格。
根据文档,应该调用OnCreateViewHolder方法NoOfCellsDisplayedFirstTime + 1
。对于这个例子,它应该是10 + 1 = 11。为什么要为位置12,13和14调用OnCreateViewHolder
方法?
这是我的适配器代码
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<Movie> moviesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, year, genre;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
genre = (TextView) view.findViewById(R.id.genre);
year = (TextView) view.findViewById(R.id.year);
}
}
public MoviesAdapter(List<Movie> moviesList) {
this.moviesList = moviesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Movie movie = moviesList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.year.setText(movie.getYear());
}
@Override
public int getItemCount() {
return moviesList.size();
}
}