android - adding swipe inside onBindViewHolder RecyclerView -
hey guys im trying use swipe inside onbindviewholder because items database think doesnt seem work cuz app crashing. im using custom cursor adapter recyclerview https://gist.github.com/skyfishjy/443b7448f59be978bc59 here code.
@override public void onbindviewholder(itemviewholder viewholder, cursor cursor) { mitems = cursor; final int id = cursor.getint(cursor.getcolumnindex(mydbhandler.column_id)); final string title = cursor.getstring(cursor.getcolumnindex(mydbhandler.column_title_reminder)); final string desc = cursor.getstring(cursor.getcolumnindex(mydbhandler.column_desc_reminder)); final string date = cursor.getstring(cursor.getcolumnindex(mydbhandler.column_date_reminder)); viewholder.title.settext(title); viewholder.itemview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(context, string.valueof(id), toast.length_short).show(); } }); itemtouchhelper.simplecallback simpleitemtouchcallback = new itemtouchhelper.simplecallback(0, itemtouchhelper.left | itemtouchhelper.right) { @override public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) { return false; } @override public void onswiped(recyclerview.viewholder viewholder, int swipedir) { int itemposition = viewholder.getadapterposition(); notifyitemremoved(itemposition); dbhandler.deletereminder(id); } }; itemtouchhelper itemtouchhelper = new itemtouchhelper(simpleitemtouchcallback); itemtouchhelper.attachtorecyclerview(this.myrecyclerview); }
is there anyway can swiping dismiss items in recyclerview?
remove following chunk of code onbindviewholder , add in activity or fragment initializing adapter.
itemtouchhelper.simplecallback simpleitemtouchcallback = new itemtouchhelper.simplecallback(0, itemtouchhelper.left | itemtouchhelper.right) { @override public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) { return false; } @override public void onswiped(recyclerview.viewholder viewholder, int swipedir) { // type cast view holder // cusrsorviewholder cviewholder = (cursorviewholder)viewholder; int itemposition = viewholder.getadapterposition(); notifyitemremoved(itemposition); dbhandler.deletereminder(cviewholder.id); } }; itemtouchhelper itemtouchhelper = new itemtouchhelper(simpleitemtouchcallback); itemtouchhelper.attachtorecyclerview(this.myrecyclerview); //put above code before following method in activity or fragment //this.recylerview.setadapter(adapter)
set id in onbindviewholder
@override public void onbindviewholder(itemviewholder viewholder, cursor cursor) { mitems = cursor; final int id = cursor.getint(cursor.getcolumnindex(mydbhandler.column_id)); viewholder.id = id; }
Comments
Post a Comment