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.