0

I have a RecyclerView and it uses a data source to load the items in the adapter in parts as the user scrolls down i.e. kind of "pagination".
I need to add items in the list from different data sources and I really can't modify the first data source I mentioned to serve everything.
The problem is: as I scroll down how do I know which data source to use and add items from there? E.g.
1. Add items from Source-A
2. User scrolls
3. Add items from Source-B
4. User scrolls
5. There are no more items from Source-A and the header for items from Source-B is visible so I should start loading from Source-B etc
Is there a clean way to implement this? I don't want to start doing checks how many data were loaded from Source-A etc. I mean is there a pattern to do this based on visibility e.g. of the header?

Jim
  • 2,341
  • 1
  • 11
  • 23
  • 1
    If you implement an adapter that takes in a list you can basically set whatever you want as the types of data to put in that list. Then you can assign that to your recycler and just update the list when you get new data – luckyging3r Apr 05 '19 at 20:16
  • https://stackoverflow.com/a/26245463/5184092 – luckyging3r Apr 05 '19 at 20:19
  • @luckyging3r: Unless I misunderstood what you are saying, my problem is not how to inflate different types of views but how I should load data from the corresponding data source for each type – Jim Apr 05 '19 at 20:33
  • 1
    I see.. Could you make an object that holds either a source A object or source B object with a type flag that says which object it is? That why you can populate a list with a Data object that holds either A or B. Forgive me if I am misunderstanding you. – luckyging3r Apr 05 '19 at 21:13
  • Maybe this post is what you are looking for https://stackoverflow.com/a/49853897/5184092 – luckyging3r Apr 05 '19 at 21:16
  • @luckyging3r: My problem is exactly how do I populate the adapter item list with items *from* multiple data sources. I know how to render them after that – Jim Apr 05 '19 at 21:17

1 Answers1

0

You can't typically feed a recyclerview adapter more than one list. You can build a list out of multiple data sources and if need be have the adapter package each type of data separately.

brandonx
  • 258
  • 2
  • 11
  • Yes the adapter can't have more than 1 list. The question is, how can I append to this (one) adapter's list from multiple sources as the I scroll down – Jim Apr 05 '19 at 22:22
  • I would need to know more about your design to understand what you are doing. In the most simple setups, it is best to add everything to the recycler list beforehand. You can of course write a method in your adapter that accepts new objects in your list at whatever index you like. Usually this would be triggered by some event like someone sending a user a chat message and you appending it to the recycler list. – brandonx Apr 05 '19 at 22:38
  • The adapter's list is populated by data from a database table-1 but not everything is put in the list but e.g 20 at a time. When the user scrolls down extra 20 are fetched from the table and added to the list (and displayed). What I want is once the data from table-1 finishes to start doing the same thing from another storage (let's call it table-2) and keep adding items on scroll till that data finishes and so on. Is this more clear? – Jim Apr 06 '19 at 08:29