0

I am trying to populate data into a recycler view at both ends(older at bottom, newer at top), like Twitter does. I have done a little bit of research and found out that Deque (Double Ended Queue) can be used to add data at both ends.

My issue and question is once data is added into the Deque, how can it be populated into a recycler view using an adapter?

  • just take the data from the database and populate it into the RecyclerView in a specific order e.g. if you're using a backend such as sashido you can get the data and order it by when it was created on the server. – Bradley Wilson Jan 09 '17 at 13:34
  • I am retrieving the data from a server and want to load it on chunks. So when the user launches the app he is presented with the initial data. Scrolling to the bottom will load older data and pulling down to refresh will load the latest data on top – Pascal Okyere Adomako Jan 09 '17 at 13:37
  • 1
    Not sure I understand the question. A RecyclerView (via its adapter) uses an underlying data store such as a List(Of x) which can have items inserted at any position. You do not need special handling to do this. – Kuffs Jan 09 '17 at 13:37
  • your questioning is very vague (your original question is to populate the RecyclerView in a specific order but the comment seems like you want to load data initially and then let the user load more), please add more information to the question such as specific problems, any code you've tried already and any other information you feel is important to make it clear. – Bradley Wilson Jan 09 '17 at 13:40
  • @Kuffs this can be achieved easily when it's added to the bottom but when new data is added to de top, it means all existing data in the arraylist will be repositioned. I guess my issues is to achieve loading of old and new data at both ends of the recycler view without losing the current position the user is on and avoid flickering when new data is added(flickering will somewhat occur when I take the approach of adding all the data again in a new instance of the arraylist) – Pascal Okyere Adomako Jan 09 '17 at 13:43
  • @BradleyWilson okay so yeah 1. I will like to present the user with initital data. 2. When the user scrolls to the bottom, older data is retrieved. 3. When the user pulls down to refresh, new data is added. I want to achieve above without resetting the entire arraylist(if an arraylist is used), to avoid flickering when the new data is set. Are there any other ways of achieving this. Hope its clear now – Pascal Okyere Adomako Jan 09 '17 at 13:51
  • its very clear now. well you can update a specific range of data using notifyItemRangeChanged(); when you refresh or load more data keep a count of how many refreshes the user has done and then refresh the range after the initial data, so if the initial data is 10 items, and when you want the user to refresh data and load another ten, you add one to the refreshCount and refresh the range from 11 - refreshCount * 10) so one refresh count = 10 items, two = 20 items etc etc so or you could load 10 at a time if you store the total items displayed in a global variable and refresh ten on top of that – Bradley Wilson Jan 09 '17 at 13:56
  • also see this: http://stackoverflow.com/questions/26543131/how-to-implement-endless-list-with-recyclerview – Kuffs Jan 09 '17 at 14:01
  • @BradleyWilson I think i get what u'r saying but assuming i initially have 10 items(position 0-9) in my arraylist and i load 5 more OLD data(now position 10-14) your suggestion will work as i am adding to the bottom of the arraylist. However if i want to load 5 more NEW data, it will have to be at the top of the arraylist right?(position 0-4) therefore the data i already have will be moved(position 5-19). Hence i will have to notify change for the whole list as the positions have been renewed. I'm i missing something?? – Pascal Okyere Adomako Jan 09 '17 at 15:40
  • just add the new data to the range you would like using notifyItemInserted() so if it it's position 0-4 for new data, just use notifyItemRangeChanged(); to notify the user that 0-4 has got new data added, it'll refresh the top 5 items, if you would like the user to see those new items (or add a button like facebook does when new items go to the news feed) just scroll them to the top automatically. – Bradley Wilson Jan 09 '17 at 16:10

0 Answers0