android - RecyclerView not updating its views when updated with new data -
i have recyclerview recyclerview.adapter implementation, has list of objects plus header , footer view, , switching on viewtype show header position 0 , footer position getitemcount() (my "getitemcount()" implementation returns "data.size() + 2" accommodate header , footer). works great, header , foot shown @ correct places, when reach end of list , footer shown, load more data , add "data" list, , want footer view replaced next loaded data view, not happen. have tried alle different "notifyx" methods on adapter, seems not updated until scroll list (and works want).
what missing here? how force recyclerview redraw last element in list?
my adapter:
private class transactionsadapter extends recyclerview.adapter { private list transactions; private static final int type_header = 0; private static final int type_item = 1; private static final int type_footer = 2; private transactionsadapter(list transactions) { this.transactions = transactions; } public void add(transaction transaction) { transactions.add(transaction); notifyiteminserted(transactions.size()); } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { if(viewtype == type_header) { viewholder vh = new viewholder(inflate(getcontext(), r.layout.postings_list_header, null), type_header); return vh; } else if(viewtype == type_footer) { viewholder vh = new viewholder(inflate(getcontext(), r.layout.postings_list_footer, null), type_footer); return vh; } slidingcellview cellview = new slidingcellview(getcontext()); cellviews.add(cellview); viewholder vh = new viewholder(cellview, type_item); return vh; } @override public void onbindviewholder(viewholder holder, final int position) { if (holder.gettype() == type_header) { final textview text = (textview) holder.itemview.findviewbyid(r.id.postings_header_text); text.settext("dette er nu godt"); } else if (holder.gettype() == type_item) { final transaction transaction = transactions.get(position - 1); ((slidingcellview) holder.itemview).update(new slidingcellview.slidingcellviewresources() { @override public string getslidingcellname() { return transaction.gettext(); } }); } else if (holder.gettype() == type_footer) { final textview text = (textview) holder.itemview.findviewbyid(r.id.no_more_postings); if(transactions.size() == 0) { text.settext(getresources().getstring(r.string.no_postings)); } else { if(resources.gettransactions().hasmore()) { text.settext(getresources().getstring(r.string.more_postings)); } else { text.settext(getresources().getstring(r.string.end_of_postings)); } } } } @override public int getitemcount() { return transactions.size() + 2; } @override public int getitemviewtype(int position) { if(position == 0) { return type_header; } if(position == transactions.size() + 1) { return type_footer; } return type_item; } public class viewholder extends recyclerview.viewholder { private int type; public viewholder(view itemview, final int type) { super(itemview); this.type = type; } public int gettype() { return type; } } }
code add transactions:
public void transactionsadded() { (transaction trans : resources.gettransactions()) { postingslistadapter.add(trans); } }
so, if scroll bottom of list, "r.layout.postings_list_footer" view correctly displayed @ bottom of list, when service call returns (code not shown) "transactionsadded" gets called , expect list redraw , draw last shown element normal item , not footer, happens when start scroll in list. thank you
søren
you can add onbindviewholder:
if (position == getitemcount() - 1) { // request more transactions }
then callback calls add method, should call notifydatasetchanged:
public void add(transaction transaction) { transactions.add(transaction); notifydatasetchanged(); }
there working example without footer here.
if need library handle header/footers in recyclerview should try sectionedrecyclerviewadapter.
Comments
Post a Comment