android - How to add loading progress at bottom of recyclerview? -
i have app contain recyclerview ads in between recyclerview , losing progress footer @ bottom of recyclerview when scroll @ bottom of recyclerview adding more data in dataset working fine problem adding loading progress @ bottom.
code of main activity:
contacts = new arraylist<>(); random = new random(); (int = 0; < 5; i++) { contact contact = new contact(); contact.setphone(phonenumbergenerating()); contact.setemail("contact" + + "@gmail.com"); contact.setviewtype(1); contacts.add(contact); } //place 2 admob ads in recyclerview contact mystring1 = new contact(); mystring1.setviewtype(2); contacts.add(3,mystring1); recyclerview.setlayoutmanager(new linearlayoutmanager(this)); contactadapter = new contactadapter(recyclerview, contacts, this); recyclerview.setadapter(contactadapter); //set load more listener recyclerview adapter contactadapter.setonloadmorelistener(new onloadmorelistener() { @override public void onloadmore() { if (contacts.size() <= 7) { contacts.add(null); contactadapter.notifyiteminserted(contacts.size() - 1); new handler().postdelayed(new runnable() { @override public void run() { contacts.remove(contacts.size() - 1); contactadapter.notifyitemremoved(contacts.size()); //generating more data int index = contacts.size(); int end = index + 2; (int = index; < end; i++) { contact contact = new contact(); contact.setphone(phonenumbergenerating()); contact.setemail("devexchanges" + + "@gmail.com"); contact.setviewtype(1); contacts.add(contact); } //place 1 admob ads in recyclerview contact mystring1 = new contact(); mystring1.setviewtype(2); contacts.add(3,mystring1); contactadapter.notifydatasetchanged(); contactadapter.setloaded(); } }, 5000); } else { toast.maketext(mainactivity.this, "loading data completed", toast.length_short).show(); } } });
code of adapter:
public int getitemviewtype(int position) { if (position == contacts.size()) { // footer_view check in switch/case return 3; } return contacts.get(position).getviewtype(); } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { recyclerview.viewholder viewholder = null; layoutinflater inflater = layoutinflater.from(parent.getcontext()); switch (viewtype) { case 1: view itemview = inflater.inflate(r.layout.item_recycler_view_row, parent, false); viewholder = new userviewholder(itemview); break; case 2: view adview = inflater.inflate(r.layout.item_three, parent, false); viewholder = new itemthree(adview); break; case 3: view loading = inflater.inflate(r.layout.item_loading, parent, false); viewholder = new loadingviewholder(loading); break; } return viewholder; } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { switch (holder.getitemviewtype()) { case 1: contact contact = contacts.get(position); userviewholder userviewholder = (userviewholder) holder; userviewholder.phone.settext(contact.getemail()); userviewholder.email.settext(contact.getphone()); break; case 2: itemthree itemthree = (itemthree) holder; itemthree.textview.settext("ads"); break; case 3: loadingviewholder loadingviewholder = (loadingviewholder) holder; loadingviewholder.progressbar.setindeterminate(true); break; } } @override public int getitemcount() { return contacts.size()+1; }
one easy way use library https://github.com/mikepenz/fastadapter
and use progress view footeradapter.
but on approach, should add type 3 contact before notifyadapterchange.
if(shouldloadmore){ contact laoder = new contact(); contact.setviewtype(3); contacts.add(loader); }
and everytime data comes check last item if type 3. replace
contacts.remove(contacts.size()-1);
with
if(contacts.size() > 0 && contacts.get(contacts.size()-1).getviewtype() == 3){ contacts.remove(contacts.size()-1); }
Comments
Post a Comment