android - AddMore button in RecyclerView is not removed -
i have recyclerview 2 viewtypes: images , 1 button. when click button i'm able add 1 more image each time. want when have 5 images, button disappear, without using setview
(gone). here adapter, , i'm trying remove button in onbindviewholder
:
public class selectphotoadapter extends recyclerview.adapter<selectphotoholder> {// recyclerview extend // recyclerview adapter private arraylist<data_model> arraylist; private context context; private boolean hasloadbutton = true; private final int images = 0; private final int load_more = 1; public selectphotoadapter (context context, arraylist<data_model> arraylist) { this.context = context; this.arraylist = arraylist; } public boolean ithasloadbutton() { return hasloadbutton; } public void sethasloadbutton(boolean hasloadbutton) { this.hasloadbutton = hasloadbutton; notifydatasetchanged(); } @override public int getitemcount() { if (hasloadbutton) { return arraylist==null? 1 :arraylist.size() + 1; } else { return arraylist==null? 0 :arraylist.size(); } } @override public int getitemviewtype(int position) { if (position < getitemcount()-1) { return images; } else { return load_more; } } @override public void onbindviewholder(selectphotoholder holder, int position) { selectphotoholder mainholder = holder;// holder if(position >= getitemcount() -1) { mainholder.addphoto.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { arraylist.add(new data_model(selectphotodialogfragment.images[0])); selectphotodialogfragment.adapter.notifyiteminserted(arraylist.size()); selectphotodialogfragment.mrecyclerview.scrolltoposition(arraylist.size()); log.d("askj","sizee: "+arraylist.size()); if(arraylist.size()>5&&ithasloadbutton()){ sethasloadbutton(false); selectphotodialogfragment.adapter.notifyitemremoved(arraylist.size() + 1); selectphotodialogfragment.adapter.notifyitemrangechanged(arraylist.size() + 1, getitemcount()); log.e("askj", "calledload"); } } }); } else { final data_model model = arraylist.get(position); bitmap image = bitmapfactory.decoderesource(context.getresources(), model.getimagepath());// convert drawbale image mainholder.imageview.setimagebitmap(image); } } @override public selectphotoholder oncreateviewholder(viewgroup parent, int viewtype) { if(viewtype == images) { return new selectphotoholder((layoutinflater.from(parent.getcontext()).inflate(r.layout.selectphotolist, parent, false)),images); } else if (viewtype == load_more) { return new selectphotoholder((layoutinflater.from(parent.getcontext()).inflate(r.layout.addphoto, parent, false)),load_more); } else { return null; } } }
the method called(i can see in console 'calledload'), button still there. thought calling sethasloadbutton
(false) button disappear, it's not happening.
let me know if have add more code.
you can change condition in getitemviewtype
position < getitemcount()-1
arraylist!=null && position < arraylist.size()
.
better move management of onclicklistener
interface directly viewholder
.
Comments
Post a Comment