android - ListView mark selected list entries onItemLongClick in Contextual ActionBar -
i've implemented listview
in sense of master detail view:onitemclick()
calls new activity
details according list item.
onitemlongclick
calls actionmode.callback
provide selection of multiple list items. solved problem avoid calling onitemclick()
when item should selected returning true
in onitemlongclick()
.
the problem selection made/is visualized after clicked item , returned activity.
please not standard listselector
. i'm not sure if correct way/solution. tried implement custom listselector
did not work properly.
any suggestions?
xml of listview
:
<?xml version="1.0" encoding="utf-8"?> <linearlayout 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" android:orientation="vertical" tools:context=".resultfragment" > <!-- standard list_selector_background --> <listview android:id="@+id/fragment_ergebnis_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:choicemode="multiplechoice" android:divider="@color/gray_3" android:dividerheight="1dp" android:listselector="@android:drawable/list_selector_background" android:saveenabled="true" > </listview> </linearlayout>
code of fragment
public class resultpojofragment extends fragment implements onitemclicklistener, onitemlongclicklistener { private activity mactivity; private actionmodecallback mactionmodecallback; private actionmode mactionmode; private listview mlistview; private resultadapter madapter; private resultdao mresultdao; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { mactivity = getactivity(); mresultdao = new resultdao(mactivity); view fragmentview = inflater.inflate(r.layout.fragment_result, container, false); madapter = new resultadapter(mactivity); mlistview = (listview) fragmentview.findviewbyid(r.id.fragment_result_listview); mlistview.setadapter(madapter); mlistview.setonitemclicklistener((onitemclicklistener) this); mlistview.setonitemlongclicklistener((onitemlongclicklistener) this); mactionmodecallback = new actionmodecallback(); return fragmentview; } @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { resultpojo result = new resultpojo(); result = madapter.getitem(position); /* show activity details */ intent resultdetailsintent = new intent(getactivity(), resultpojodetailsactivity.class); if (resultdetailsintent != null) { resultdetailsintent.putextra(appconstants.tag_result, result); startactivity(resultdetailsintent); } } @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long id) { if (mactionmode != null) { /* true: avoid calling onitemclick() */ return true; } else { mactionmode = mactivity.startactionmode(mactionmodecallback); view.setselected(true); } mactionmode = mactivity.startactionmode(new actionmodecallback()); return true; } private void selectallentries() { (int = 0; < mlistview.getchildcount(); i++) { mlistview.setitemchecked(i, true); } } private void deselectallentries() { (int = 0; < mlistview.getchildcount(); i++) { mlistview.setitemchecked(i, false); } } private void refreshview() { madapter.notifydatasetchanged(); } private class actionmodecallback implements actionmode.callback { @override public boolean oncreateactionmode(actionmode mode, menu menu) { menuinflater inflater = mode.getmenuinflater(); inflater.inflate(r.menu.result_fragment_cab, menu); return true; } @override public boolean onprepareactionmode(actionmode mode, menu menu) { return true; } @override public boolean onactionitemclicked(actionmode mode, menuitem item) { switch (item.getitemid()) { case r.id.menu_item_cab_result_fragment_select_all: selectallentries(); break; case r.id.menu_item_cab_result_fragment_deselect_all: deselectallentries(); break; default: break; } return false; } @override public void ondestroyactionmode(actionmode mode) { mactionmode = null; } } }
Comments
Post a Comment