82

I am a beginner and I am having trouble with understanding a piece of code. Can someone please explain me when this function evoke and what is it for?

Here is my code :

    public void onBindViewHolder(myViewHolder holder, int position) {

        RecViewHolder currentdata = data.get(position);
        holder.favChecker = currentdata.getFavChecker();
        holder.serialID = currentdata.getSerialID();
        holder.theClassName = currentdata.getTheClassName();
}
Shunan
  • 3,165
  • 6
  • 28
  • 48
  • 1
    check this hope it will help you https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html – Pradeep Gupta May 30 '16 at 10:41
  • thanks @PradeepGupta but i am still confused when it is called. – Shunan May 30 '16 at 10:46
  • Your code is a little strange, because it looks like you are storing things in the `ViewHolder` other than views. using the normal `ViewHolder` pattern you would only store views inside a `ViewHolder`, so your code would be something more like `holder.tvSerialIdField.setText(currentData.getSerialID())` – Adam Burley May 11 '23 at 14:38

2 Answers2

214

Let me start with just a little bit of background (which you may already understand, but it's needed to explain onBindViewHolder()).

RecyclerView is designed to display long lists (or grids) of items. Say you want to display 100 rows of something. A simple approach would be to just create 100 views, one for each row and lay all of them out. But that would be wasteful, because most of them would be off screen, because lets say only 10 of them fit on screen.

So RecyclerView instead creates only the 10 views that are on screen. This way you get 10x better speed and memory usage. But what happens when you start scrolling and need to start showing next views?

Again a simple approach would be to create a new view for each new row that you need to show. But this way by the time you reach the end of the list you will have created 100 views and your memory usage would be the same as in the first approach. And creating views takes time, so your scrolling most probably wouldn't be smooth.

This is why RecyclerView takes advantage of the fact that as you scroll and new rows come on screen also old rows disappear off screen. Instead of creating new view for each new row, an old view is recycled and reused by binding new data to it.

This happens exactly in onBindViewHolder(). Initially you will get new unused view holders and you have to fill them with data you want to display. But as you scroll you'll start getting view holders that were used for rows that went off screen and you have to replace old data that they held with new data.

Marcin Koziński
  • 10,835
  • 3
  • 47
  • 61
  • 4
    with upgrade in SDK now I got different behaviour i.e. onBindViewHolder is contentiously calling. – Abhishek May 16 '17 at 06:36
  • 1
    @Abhishek I think that's my observation to the behavior of `onBindViewHolder` as well. I'm trying to code an app which lists arbitrary number of items with images (thousands of items), and when I logged the calls to `onBindViewHolder`, all the thousands of items where logged. – patrickjason91 Jun 23 '17 at 09:20
  • @patrickjason91 I gone through some links. There are some circumstances like when we used Recycler View Inside Scroll View OnBindViewHolder getting called continuously, in some SDK also recycler views not works as expected so better we have to use stable recycler view support. – Abhishek Jun 23 '17 at 10:10
  • @Abhishek While searching through some questions and answers, some claim that the issue is due to newer versions of `RecyclerView`, and also the issue occurs on Pre-Lollipop devices. I'm testing with old version/prelollipop devices EDIT: I now tested to a post-lollipop device, nougat, and issue still persists – patrickjason91 Jun 23 '17 at 10:13
  • @Marcin, you said "RecyclerView instead creates only the 10 views that are on screen", at what moment is the 11th view onBindViewHolder() called? i.e., How much of the 10th item should be visible? – Kiran Oct 12 '17 at 12:40
  • I don't know unfortunately. I think that depends on the LayoutManager you're using - it's an implementation detail so it might vary. – Marcin Koziński Oct 13 '17 at 13:52
  • @MarcinKoziński well explained. May I know where do you find this explaination and how does `onBindViewHolder` or `onCreateViewHolder` do? – Wee Hong Jun 08 '18 at 15:28
  • This explanation is good only for one ViewHolder. If you have several, a mechanism is a bit more difficult. – CoolMind Apr 30 '19 at 08:14
  • What about `DiffUtil.ItemCallback diffCallback` which could be provided inside adapter constructor. If we change element, that already has been bound, `onBindViewHolder` will be called again? – Falcon Nov 26 '21 at 09:57
7

It is called by RecyclerView to display the data at the specified position. This method is used to update the contents of the itemView to reflect the item at the given position.

for more info check RecyclerView.Adapter#onBindViewHolder

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23