android - RecyclerView is not showing all of the items in the adapter -
i implemented infinite scrolling in recyclerview
checking item's position in onbindviewholder()
, whether or not more items being requested rest
service. if item's position less 5 end of list , request not being made more items, request more items executed.
@override public void onbindviewholder(itemholder holder, int position) { holder.bindholder(position); //debugging purposes //logs current position, size of item list, , whether //or not more items being retrieved rest service log.d("adapter", "position = " + position + "\nmitems.size() = " + mitems.size() + "\nget_user_feed_is_inactive = " + httprequests.get_user_feed_is_inactive + "\n\n"); //query more items if user less 5 items end , //there not active query if (position > mpolls.size() - 5 && httprequests.get_user_feed_is_inactive){ httprequests.getusersfeed(); } }
infinite scrolling works fine if recyclerview
scrolled through slowly, if scroll end quickly, query grabs next batch of items, adds them list, recyclerview
won't move past item used lat item if it's end of list. crazy part it's clear logging list larger recyclerview
makes seem appear, won't display new items.
the below 4 logs last 4 created when scroll bottom of recyclerview
quickly:
d/adapter: position = 20 mitems.size() = 50 get_user_feed_is_inactive = true d/adapter: position = 19 mitems.size() = 50 get_user_feed_is_inactive = true d/adapter: position = 23 mitems.size() = 50 get_user_feed_is_inactive = true d/adapter: position = 24 mitems.size() = 50 get_user_feed_is_inactive = true
the last log shows onbindviewholder()
called item @ position 24 -- last item received first query -- , @ time, mitems.size()
50 -- second batch of 25 items has been received , added mitems
. however, cannot scroll down further past item 24.
any ideas on why may happening?
.
update:
here code runs when receive response rest
service:
public void onresponse(string response) { list<item> usersfeed = sgson.fromjson(response, new typetoken<arraylist<item>>(){}.gettype()); //get size of adapter's list before new items added int initialnumberofitemsinadapter = fuserfeed.sadapter.getitemcount(); //add new items adapter's list realmsingleton.addtobottomofuserfeedrealm(usersfeed); //notify adapter of new items fuserfeed.sadapter .notifyitemrangeinserted(initialnumberofitemsinadapter, usersfeed.size()); //signify end of getuserfeed activity get_user_feed_is_inactive = true; log.d("volley", response); }
update: more strange behavior -- when navigate fragment , right user feed fragment, recyclerview
recognizes there more items in list, infinite scrolling starts behaving again. if scroll down again, bug reoccurs, , have navigate , fragment working again.
it seems problem realmsingleton.addtobottomofuserfeedrealm(usersfeed)
adding item
s adapter's realmresults
list asynchronously -- feature of realm
-- , calling fuserfeed.sadapter.notifyitemrangeinserted()
right after it. so, notifying adapter new items inserted before new items inserted.
so did addchangelistener(realmchangelistener)
mitems
, called notifyitemrangeinserted()
within ensure it's called after mitems
has changed.
now it's working perfectly; no matter how fast scroll down.
it still weird though why worked when scrolled slowly. scrolling speed doesn't affect speed @ realm
asynchronously added items mitems
, notifyitemrangeinserted()
should have been called before items added list. , yet, implementation still worked if scrolled sufficiently slowly.
Comments
Post a Comment