android - ListView selection does not work correct -
i've implemented fragment listview has contextual actionbar.
onlongclick starts contextual actionmode select items of listview , delete selected items. part works fine. i'm not able select first entry onlongclick - that's minor problem.
the 'normal' onclick opens activity details according listview entry. opening new activity works fine. problem is: onclick selects according listview entry.
how can solve this. suggestions?
xml of fragment:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.test.resultfragment" > <listview android:id="@+id/fragment_result_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:choicemode="multiplechoice" android:divider="@color/ktbl_gray_3" android:dividerheight="1dp" android:listselector="@drawable/selectable_background" android:saveenabled="true" android:smoothscrollbar="true" > </listview> <textview android:id="@+id/fragment_result_textview_empty" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:text="no entries" android:textcolor="@color/android_icon_dark" android:textsize="16sp" android:visibility="gone" /> </framelayout> the selector:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitfadeduration="@android:integer/config_mediumanimtime"> <item android:drawable="@drawable/list_focused" android:state_focused="true" android:state_pressed="false"/> <item android:drawable="@drawable/list_focused" android:state_activated="true"/> <item android:drawable="@drawable/list_pressed" android:state_pressed="true"/> <item android:drawable="@android:color/transparent"/> </selector> the shortend version of xml of row:
<gridlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/activatedbackgroundindicator" android:columncount="4" android:orientation="horizontal" android:padding="@dimen/listview_padding" android:rowcount="4" > <!-- etc. ... --> </gridlayout> the code:
public class resultfragment extends fragment implements onitemclicklistener, onitemlongclicklistener { private static final string tag = resultfragment.class.getname(); private activity mactivity; private actionmodecallback mactionmodecallback; private actionmode mactionmode; private listview mlistview; private resultadapter madapter; private resultdao mresultdao; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); log.i(tag, "onactivitycreated"); setretaininstance(true); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.i(tag, "oncreateview"); mactivity = getactivity(); mactionmodecallback = new actionmodecallback(); mresultdao = new resultdao(mactivity); view fragmentview = inflater.inflate(r.layout.myfragment, container, false); setupview(fragmentview); return fragmentview; } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { super.oncreateoptionsmenu(menu, inflater); log.i(tag, "oncreateoptionsmenu"); inflater.inflate(r.menu.result_fragment, menu); } @override public boolean onoptionsitemselected(menuitem item) { log.i(tag, "onoptionsitemselected"); switch (item.getitemid()) { case r.id.menu_item_result_fragment_help: startactivity(new intent(getactivity(), resulthelpactivity.class)); break; default: log.w(tag, "onoptionsitemselected - default case"); break; } return super.onoptionsitemselected(item); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { log.w(tag, "onitemclick"); if (mactionmode == null) { /* no contextual actionbar active - show detail activity */ result result = madapter.getitem(position); intent resultdetailsintent = new intent(mactivity, resultdetails.class); resultdetailsintent.putextra(appconstants.tag_result, result); startactivity(resultdetailsintent); } else { /* contextual actionbar active - selectie/deselect listview entries */ mlistview.setitemchecked(position, mlistview.isitemchecked(position)); updatecabtitle(mactionmode); } } @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long id) { log.w(tag, "onitemlongclick"); if (mactionmode == null) { mactionmode = mactivity.startactionmode(mactionmodecallback); /* false : onitemlongclick not consumed --> open onitemclick() */ return false; } /* true : onitemlongclick consumed --> nothing */ return true; } private void setupview(view fragmentview) { log.i(tag, "setupview"); sethasoptionsmenu(true); madapter = new resultadapter(mactivity); mlistview = (listview) fragmentview.findviewbyid(r.id.fragment_result_listview); mlistview.setadapter(madapter); mlistview.setonitemclicklistener((onitemclicklistener) this); mlistview.setonitemlongclicklistener((onitemlongclicklistener) this); /* set emptyview */ textview emptyview = (textview) fragmentview.findviewbyid(r.id.fragment_result_textview_empty); mlistview.setemptyview((view) emptyview); } private void selectallentries() { log.i(tag, "selectallentries"); (int = 0; < mlistview.getcount(); ++i) { mlistview.setitemchecked(i, true); } updatecabtitle(mactionmode); } private void deselectallentries() { log.i(tag, "deselectallentries"); (int = 0; < mlistview.getcount(); ++i) { mlistview.setitemchecked(i, false); } updatecabtitle(mactionmode); } private void deleteselectedentries() { log.i(tag, "deleteselectedentries"); sparsebooleanarray checkeditems = mlistview.getcheckeditempositions(); (int = 0; < checkeditems.size(); ++i) { if (checkeditems.valueat(i)) { result result = (result) mlistview.getitematposition(i); if (result != null) { mresultdao.deleteresult(result); } } } refreshview(); } private void refreshview() { log.i(tag, "refreshview"); mactivity.runonuithread(new runnable() { @override public void run() { madapter.notifydatasetchanged(); } }); } private void updatecabtitle(actionmode actionmode) { log.i(tag, "updatecabtitle"); if (actionmode != null) { int numberofcheckeditems = mlistview.getcheckeditemcount(); actionmode.settitle(string.valueof(numberofcheckeditems) + " " + getstring(r.string.selektiert)); } } private class actionmodecallback implements actionmode.callback { /** called when action mode created. called startactionmode() */ @override public boolean oncreateactionmode(actionmode mode, menu menu) { log.i(tag, "oncreateactionmode"); menuinflater inflater = mode.getmenuinflater(); inflater.inflate(r.menu.result_fragment_cab, menu); updatecabtitle(mode); return true; } /** invoked whenever action mode shown. invoked after oncreateactionmode */ @override public boolean onprepareactionmode(actionmode mode, menu menu) { log.i(tag, "onprepareactionmode"); return true; } @override public boolean onactionitemclicked(actionmode mode, menuitem item) { log.i(tag, "onactionitemclicked"); switch (item.getitemid()) { case r.id.menu_item_cab_result_fragment_delete_selected: deleteselectedentries(); /* close contextual actionbar */ mode.finish(); return true; case r.id.menu_item_cab_result_fragment_select_all: selectallentries(); return true; case r.id.menu_item_cab_result_fragment_deselect_all: deselectallentries(); return true; default: log.w(tag, "onactionitemclicked - default case"); return false; } } /** called when user exits action mode */ @override public void ondestroyactionmode(actionmode mode) { log.w(tag, "ondestroyactionmode"); mactionmode = null; deselectallentries(); } } }
Comments
Post a Comment