0

I am looking to modify the number of items that is displayed in my recyclerviewer on the event that the last item of the current itemcount is viewed..

   var mainadapt = MainAdapter()
        Timeline.adapter = mainadapt
        
        fun OnLastItemReached(){
            Timeline.adapter.itemCount += 10
        }

The code above is incorrect since the method ".itemcount" isn't a class variable so it can't just be modified at will. But it is the logic that I am trying to showcase, Basically loading 10 more items every time the last items is viewed.

RonRon Scores
  • 163
  • 2
  • 11
  • i can help you with java not kotlin(if you want) you can convert it into java then – Jyotish Biswas Aug 06 '20 at 02:57
  • please , i would very much appreciate it. can I messenger you? – RonRon Scores Aug 06 '20 at 02:58
  • before that tell me what you exactly want in breaf. are trying to do manual pagination with Adapter? – Jyotish Biswas Aug 06 '20 at 03:33
  • yes , im trying to just load more items into recycleviewer when the last item is reached.. like if create a class variable inside of my adapter class : var itemcountvar = 20 – RonRon Scores Aug 06 '20 at 03:47
  • when the position of the onbindviewholder = 20 i will load 10 more – RonRon Scores Aug 06 '20 at 03:47
  • this way it isn't too many items being created at once. – RonRon Scores Aug 06 '20 at 03:48
  • Add a scroll listener to the layout manager, inside the layout manager has a method for knowing the las visible item, also you can get the position of it, so it is basically: is last visible item position is greater than the adapter item count minus yout threshold then add more items – cutiko Aug 06 '20 at 04:14
  • 3
    Does this answer your question? [kotlin RecyclerView Pagination](https://stackoverflow.com/questions/51433106/kotlin-recyclerview-pagination) – kelvin Aug 06 '20 at 04:38

1 Answers1

0

This Answer i am giving you in Java as you r requested in comment

use getItemCount like this and call loadPage page method from responsible Activity or Fragment with page number

int initialSize =20;
int loadSize=10;
int pageNumber =1;
@Override
public int getItemCount() {
    if (infos.size()<initialSize){
        return infos.size();
    }
    else if (infos.size()>initialSize+(pageNumber*loadSize)){
        return initialSize+(pageNumber*loadSize);
    }else {
        return infos.size();
    }
   
}

public void loadPage(int page){
    pageNumber =page;
    notifyDataSetChanged();
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jyotish Biswas
  • 674
  • 5
  • 11
  • im really just trying to have a class variable, and then have that class variable the return value for the getitemcount() , I can't find a way to modify the class variable , unless its from where the object was initilized , which is the class linked to the activity. – RonRon Scores Aug 06 '20 at 04:29
  • you can call `loadPage` from `onBindViewHolder` when the last position binds – Jyotish Biswas Aug 06 '20 at 04:37
  • https://stackoverflow.com/a/40728909/12676247 you can get that from here – Jyotish Biswas Aug 06 '20 at 04:57
  • recreate means starting the activity from beginning so everything under that activity will start from biginng – Jyotish Biswas Aug 06 '20 at 06:07