2

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
Mohammad Sommakia
  • 1,773
  • 3
  • 15
  • 48
  • you should use **RecyclerView.OnScrollListener** to load more items. – Krishna Sharma Aug 18 '18 at 18:36
  • same result now working ... – Mohammad Sommakia Aug 18 '18 at 18:48
  • Add log to your `onCreateViewHolder` method - if it's ever called more than (number of visible items + 2) times, you probably have a layout problem where `RecyclerView` height is unrestricted so it lays out all possible items at once instead of recycling the views. – Pawel Aug 19 '18 at 00:40

2 Answers2

1

finally i found the solution i have to implement nestedScrollListener because scrolling behaviors for the recyclerview not working of it's inside NestedScrollView

Mohammad Sommakia
  • 1,773
  • 3
  • 15
  • 48
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.
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • What if I override both onBindViewHolder() as well as onViewAttachedToWindow ? Will it cause any issue ? – pandey_shubham Jun 11 '23 at 07:54
  • 1
    @pandey_shubham, I suppose, no issue. `onBindViewHolder` can do some work (and can be called very rarely), while `onViewAttachedToWindow` will refresh data. – CoolMind Jun 12 '23 at 20:45