java - Add item on RecycleView Android -
i'm trying add item on recycleview this:
i have object called image
public class image{ public string name; public string url; }
when click on button add new item, code this:
@override public void additemonlist(string url) { adapter.additem(new imageobject("aaaaaa", r.drawable.close_button)); }
that method inserts on list.
public void additem(imageobject imageobject) { itemlist.add(imageobject); notifydatasetchanged(); }
my problem when click first time in additem onbindviewholder
doesn't iterate list when check in debug mod getitemcount
correct! if try add item after first time work well.
mainactivity
public class mainactivity extends appcompatactivity implements recyclerviewclicklistener, onstartdraglistener { private recyclerviewadapter adapter; private arraylist<imageobject> listviewitems; private recyclerview recyclerview; private itemtouchhelper itemtouchhelper; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); recyclerview = (recyclerview) findviewbyid(r.id.recycler_view); recyclerview.sethasfixedsize(true); staggeredgridlayoutmanager staggeredlayoutmanager = new staggeredgridlayoutmanager(3, 1); recyclerview.setlayoutmanager(staggeredlayoutmanager); list<imageobject> staggered = getlistitemdata(); adapter = new recyclerviewadapter(this, staggered); recyclerview.setadapter(adapter); itemtouchhelper.callback callback = new simpleitemtouchhelpercallback(adapter); itemtouchhelper = new itemtouchhelper(callback); itemtouchhelper.attachtorecyclerview(recyclerview); } private list<imageobject> getlistitemdata() { listviewitems = new arraylist<imageobject>(); listviewitems.add(new imageobject("alkane", r.drawable.one)); listviewitems.add(new imageobject("ethane", r.drawable.two)); listviewitems.add(new imageobject("alkyne", r.drawable.three)); listviewitems.add(new imageobject("benzene", r.drawable.four)); listviewitems.add(new imageobject("alkane", r.drawable.one)); listviewitems.add(new imageobject("ethane", r.drawable.two)); listviewitems.add(new imageobject("alkyne", r.drawable.three)); listviewitems.add(new imageobject("benzene", r.drawable.four)); listviewitems.add(new imageobject("alkane", r.drawable.one)); listviewitems.add(new imageobject("ethane", r.drawable.two)); listviewitems.add(new imageobject("alkyne", r.drawable.three)); listviewitems.add(new imageobject("benzene", r.drawable.four)); return listviewitems; } @override public void addbuttonclicked() { fragmentmanager fm = getsupportfragmentmanager(); alertfragment.newinstance(this).show(fm, "dialog"); } @override public void additemonlist(string url) { adapter.additem(new imageobject("aaaaaa", r.drawable.close_button)); } @override public void onstartdrag(recyclerview.viewholder viewholder) { itemtouchhelper.startdrag(viewholder); } }
my adapter
public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.viewholders> implements itemtouchhelperadapter { private list<imageobject> itemlist; private mainactivity mainactivity; private recyclerviewclicklistener recyclelistener; private onstartdraglistener dragstartlistener; public recyclerviewadapter(mainactivity mainactivity, list<imageobject> itemlist) { this.itemlist = itemlist; this.mainactivity = mainactivity; this.recyclelistener = mainactivity; this.dragstartlistener = mainactivity; } @override public viewholders oncreateviewholder(viewgroup parent, int viewtype) { view layoutview; switch (viewtype) { case 1: layoutview = layoutinflater.from(parent.getcontext()).inflate(r.layout.add_item_list, null); break; default: layoutview = layoutinflater.from(parent.getcontext()).inflate(r.layout.item_list, null); break; } return new viewholders(layoutview); } @override public void onbindviewholder(final viewholders holder, final int position) { switch (holder.getitemviewtype()) { case 1: configureviewholderadditem(holder, position); break; case 0: configuredefaultviewholder(holder, position); break; } } private void configureviewholderadditem(final viewholders holder, final int position) { holder.additemlayout = holder.itemview.findviewbyid(r.id.add_item_layout); holder.additemlayout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { recyclelistener.addbuttonclicked(); } }); } private void configuredefaultviewholder(final viewholders holder, final int position) { log.e("test", "position - " + position); log.e("test", "name - " + itemlist.get(position).getname()); holder.itemlayout = holder.itemview.findviewbyid(r.id.main_layout); holder.removeitembutton = holder.itemview.findviewbyid(r.id.remove_item_button); holder.itemlayout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { itemlist.remove(holder.getlayoutposition()); notifyitemremoved(holder.getlayoutposition()); notifyitemrangechanged(holder.getlayoutposition(), itemlist.size()); } }); holder.itemlayout.setbackgroundresource(itemlist.get(position).getphoto()); holder.itemlayout.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_down) { dragstartlistener.onstartdrag(holder); } return false; } }); //resizethefirstposition(holder, position); } private void resizethefirstposition(viewholders holder, int position) { if (position == 0) { int dimensionindp = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, 200, mainactivity.getresources().getdisplaymetrics()); holder.itemlayout.getlayoutparams().height = dimensionindp; holder.itemlayout.getlayoutparams().width = dimensionindp; holder.itemlayout.requestlayout(); } } public void additem(imageobject imageobject) { itemlist.add(imageobject); notifydatasetchanged(); } @override public int getitemviewtype(int position) { return position == itemlist.size() - 1 ? 1 : 0; } @override public int getitemcount() { return this.itemlist.size(); } @override public boolean onitemmove(int fromposition, int toposition) { collections.swap(itemlist, fromposition, toposition); notifyitemmoved(fromposition, toposition); return true; } @override public void onitemdismiss(int position) { itemlist.remove(position); notifyitemremoved(position); } public class viewholders extends recyclerview.viewholder implements view.onclicklistener, itemtouchhelperviewholder { private imageview removeitembutton; private imageview additembutton; private framelayout additemlayout; private relativelayout itemlayout; public viewholders(view itemview) { super(itemview); } @override public void onclick(view view) { itemlist.remove(getlayoutposition()); //notifyitemremoved(getlayoutposition()); notifyitemrangechanged(getlayoutposition(), itemlist.size()); toast.maketext(mainactivity, "removed item- " + getlayoutposition(), toast.length_short).show(); } @override public void onitemselected() { itemview.setbackgroundcolor(color.ltgray); } @override public void onitemclear() { itemview.setbackgroundcolor(0); } }
finally, fixed this. don't know why change how insert on list
old code:
public void additem(imageobject imageobject) { itemlist.add(imageobject); notifydatasetchanged(); }
new code:
public void additem(imageobject imageobject) { itemlist.add(itemlist.size() - 1, imageobject); notifydatasetchanged(); }
Comments
Post a Comment