1

The recyclerView list size is 16. On my phone, 9 and a half cell is displayed on the screen first time, It means 10 times the OnCreateViewHolder method is called.

After that if i scroll, 4 times the OnCreateViewHolder method is called; it means it is not reusing the free cell. For last two cells, OnCreateViewHolder is not called; it means now it is reusing the cell.

According to the documentation, OnCreateViewHolder method should be called NoOfCellsDisplayedFirstTime + 1. For this example it should be 10+1 =11. Why it is calling OnCreateViewHolder method for postion 12, 13 and 14?

Here is my adapter code

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();
    }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
F. Shahid
  • 101
  • 10
  • 1
    it does not have to be 10+1 - it can generate a few more `ViewHolder`s and you dont know that number (and should not know) – pskink Apr 16 '18 at 06:30
  • but all the documentations of recycler view says that it generate only one more than the screen cells because it's memory efficient, If it generates unknown number of cells , whats the advantage over normal listview – F. Shahid Apr 17 '18 at 06:58
  • it can generate extra 3 or 4 `ViewHolder`s, do you see any problem with it? – pskink Apr 17 '18 at 07:00
  • Don't have any problem programmatically , but those extra 3 4 are memory wastage. Just trying to understand why it generates extra 3 4 if it's said to be memory efficient and reuse cells – F. Shahid Apr 17 '18 at 07:37

0 Answers0