-1

I read this post When onBindViewHolder is called and how it works? and official documentation but I have not found answers to my question. onBindViewHolder is called every single time you need to bind a datum, I put a log with a variable that increments every time he is called and the word to which he must bind:

public class WordListAdapter extends RecyclerView.Adapter<WordListAdapter.WordViewHolder> {

    int x;

    ...

    @Override
    public void onBindViewHolder(WordViewHolder holder, int position) {

        WordItem current = mDB.query(position);
        holder.wordItemView.setText(current.getWord());

        Log.d("XLOGGIN", "word=" + current.getWord() + " " + "x=" + x);
        x++;

    }

    ...

}

this is the log:

Log before scroll
2019-04-26 10:15:18.064 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=Adapter x=0
2019-04-26 10:15:18.150 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=Android x=1
2019-04-26 10:15:18.213 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=Android Performance x=2
2019-04-26 10:15:18.281 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=Android Studio x=3
2019-04-26 10:15:18.346 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=Androidx x=4
2019-04-26 10:15:18.410 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=AsyncTask x=5
2019-04-26 10:15:18.474 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=Data model x=6

Log after scroll
2019-04-26 10:15:36.979 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=ListView x=7
2019-04-26 10:15:37.059 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=OnClickListener x=8
2019-04-26 10:15:37.142 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=SQLOpenHelper x=9
2019-04-26 10:15:37.219 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=SQLiteDatabase x=10
2019-04-26 10:15:37.248 19001-19001/com.adc.wordlistsql D/XLOGGIN: word=ViewHolder x=11

onBindViewHolder is called by RecyclerView, so whenever it has to bind, it is called; how does RecyclerView know when it should not call OnBindViewHolder anymore because there are no more data to bind to? In my code I have found nothing to which this behavior can be attributed. In the code there is only the database query and the wordItemView setText.

2 Answers2

1

onBindViewHolder() is call each time you need to fill one entry. If your data size is for example 10, when you scroll the list,the method is going to be called 10 times. If you scroll up again, the RecyclerView will call more times the method updating the data for the view.

The important thing is that you need to manage which data is going to be placed in each position. In your example, x only increment so there will be different data each time the onBindViewHolder() method will be called.

The size of the list is determined by the getItemCount() method.

Hope it helps.

Juanje
  • 1,235
  • 2
  • 11
  • 28
0

Recyclerview is similar to listview , the one significant difference is that reyclerview will only load items that are currently visible on screen, thereby having extremely good time and space complexity when it comes to load lot of data in the list.

Cassius
  • 353
  • 3
  • 16