android - Recyclerview selected item highlight not works -
i have used recyclerview swipe delete undo library. works fine,but recycler view item selection not works. tried change background color of selected list item.but not works. how show selected item without affecting swipe delete option? mistake have done on following code?
myactivity.java:
mylistadapter = new mylistadapter(this,null); mrecyclerview.setadapter(mylistadapter); final swipetodismisstouchlistener touchlistener = new swipetodismisstouchlistener( new recyclerviewadapter(mrecyclerview),new swipetodismisstouchlistener.dismisscallbacks() { @override public boolean candismiss(int position) { return true; } @override public void ondismiss(viewadapter viewadapter, int position) { //mrecyclerview.removeviewat(position); } }); mrecyclerview.setontouchlistener(touchlistener); // setting scroll listener required ensure during listview scrolling, // don't swipes. mrecyclerview.setonscrolllistener((recyclerview.onscrolllistener) touchlistener.makescrolllistener()); //mrecyclerview.setactivated(true); //mrecyclerview.setbackgroundresource(r.drawable.list_selector); mrecyclerview.addonitemtouchlistener(new swipeableitemclicklistener(this,new com.hudomju.swipe.onitemclicklistener() { @override public void onitemclick(view view, int position) { log.i(tag, "selected position -> " + position); view.setactivated(true); view.setenabled(true); view.setselected(true); view.setpressed(true); //view.setbackgroundcolor(r.drawable.list_selector); if(view.getid() == r.id.list_item_backside_delete_button){ // done operations on delete button click touchlistener.processpendingdismisses(); } else if(view.getid() == r.id.list_item_backside_cancel_button){ // done operations on cancel button click touchlistener.undopendingdismiss(); } } }));
mylistadapter.java:
public class mylistadapter extends cursorrecyclerviewadapter<mylistadapter.viewholder>{ public mylistadapter(context context,cursor cursor){ super(context,cursor); } public static class viewholder extends recyclerview.viewholder { public textview mtextview1; public viewholder(view view) { super(view); mtextview1 = (textview) view.findviewbyid(r.id.textview_name); /*view.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { } });*/ } } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view itemview = layoutinflater.from(parent.getcontext()) .inflate(r.layout.list_item_view, parent, false); viewholder vh = new viewholder(itemview); return vh; } @override public void onbindviewholder(viewholder viewholder, cursor cursor) { if(cursor != null && cursor.getcount() > 0){ string selectedid = cursor.getstring(cursor.getcolumnindex(dbcontract.data._id)); string name = cursor.getstring(cursor.getcolumnindex(dbcontract.data.column_name_name)); viewholder.mtextview1.settext(name); } } }
list_item_view.xml:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="20dp" android:paddingright="20dp" android:background="@drawable/list_selector" > </framelayout>
list_selector.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_activated="false" android:drawable="@android:color/transparent" /> <item android:state_activated="true" android:drawable="@color/accent" /> <item android:drawable="@android:color/transparent" /> </selector>
make global variable store position , handle click listener in viewholder. onclick of item,change global position value
textview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { globalposition=getadapterposition(); notifydatasetchanged(); } });
then in onbindviewholder
if(postion==globalposition) { //change color textview.settextcolor(color.red); } else { //revert regular color textview.settextcolor(color.white); }
with code item clicked red colored , other wiil in white
Comments
Post a Comment