'Android: OnbindViewHolder doesn't call when i scroll

Hi i am facing a strange problem when the recyclerview create first time OnbindViewHolder called until last item in recyclerview then when i scroll OnbindViewHolder not Called i need to solve this problem because i need to use pagination but when use it it called all pages from the first time this is OnbindViewHolder function

@Override
    public void onBindViewHolder(@NonNull EnglishNewsViewHolder holder, int position) {
        Log.d("bbb", holder.getAdapterPosition() + "");
        if ((((getItemCount()) - 1) - holder.getLayoutPosition() < 5) && !noMoreDate) {

            loadMoreDate();
        }

and this is how i add the items to the adapter

 public void add(NewsModel model, int i) {
        dataList.add(model);
        notifyItemInserted(i);
    }

and this is the Log after creating view immediately without scrolling.

08-18 19:58:49.958 7956-7956/qatar2022.com.qatar2022 D/bbb: 0
08-18 19:58:49.979 7956-7956/qatar2022.com.qatar2022 D/bbb: 1
08-18 19:58:49.991 7956-7956/qatar2022.com.qatar2022 D/bbb: 2
08-18 19:58:50.006 7956-7956/qatar2022.com.qatar2022 D/bbb: 3
08-18 19:58:50.020 7956-7956/qatar2022.com.qatar2022 D/bbb: 4
08-18 19:58:50.032 7956-7956/qatar2022.com.qatar2022 D/bbb: 5
08-18 19:58:50.047 7956-7956/qatar2022.com.qatar2022 D/bbb: 6
08-18 19:58:50.062 7956-7956/qatar2022.com.qatar2022 D/bbb: 7
08-18 19:58:50.077 7956-7956/qatar2022.com.qatar2022 D/bbb: 8
08-18 19:58:50.095 7956-7956/qatar2022.com.qatar2022 D/bbb: 9
08-18 19:58:50.112 7956-7956/qatar2022.com.qatar2022 D/bbb: 10
08-18 19:58:50.126 7956-7956/qatar2022.com.qatar2022 D/bbb: 11
08-18 19:58:50.139 7956-7956/qatar2022.com.qatar2022 D/bbb: 12
08-18 19:58:50.152 7956-7956/qatar2022.com.qatar2022 D/bbb: 13
08-18 19:58:50.164 7956-7956/qatar2022.com.qatar2022 D/bbb: 14
08-18 19:58:50.178 7956-7956/qatar2022.com.qatar2022 D/bbb: 15
08-18 19:58:50.194 7956-7956/qatar2022.com.qatar2022 D/bbb: 16
08-18 19:58:50.205 7956-7956/qatar2022.com.qatar2022 D/bbb: 17
08-18 19:58:50.215 7956-7956/qatar2022.com.qatar2022 D/bbb: 18


Solution 1:[1]

If you use RecyclerView in RecyclerView, see also recyclerView does not call the onBindViewHolder when scroll in the view.

You should bind ViewHolder in onViewAttachedToWindow instead of onBindViewHolder like this:

// Remove onBindViewHolder because it is often not called
//override fun onBindViewHolder(holder: SomeViewHolder, position: Int) {
//    super.onBindViewHolder(holder, position)
//}

override fun onViewAttachedToWindow(holder: SomeViewHolder) {
    super.onViewAttachedToWindow(holder)

    val position = holder.adapterPosition
    val item = items[position]
    // Refresh inner list or bind ViewHolder.
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1