Android : MultiChoiceModeListener not invoked inside ListFragment
-
09-06-2021 - |
سؤال
For an ICS based application, I have created a ListFragment, which in turn uses a BaseAdapter implementation. I have enabled MultiChoiceModeListener() in order to display the Contextual Action Bar. But the issue here is that whenever I check the CheckBox or long press the Label(Both are in the View set in BaseAdapter), MultiChoiceModeListener implementation is not invoked at all. Any sort of help is much appreciated as I am completely stuck after trying many options!!!
public class ActivitiesFragment extends ListFragment {
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "Entering onActivityCreated()");
super.onActivityCreated(savedInstanceState);
this.setAdapter();
this.setHasOptionsMenu(true);
}
private void setAdapter() {
HashMap<String, String> activities = DBAdapter
.getInstance(this.context).getActivities();
setListAdapter(new ActivitiesList(Util.sortByComparator(activities)));
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiSelectionListener());
}
private class ActivitiesList extends BaseAdapter {
// Other functions declared
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ActivityView view = null;
String activityName = this.activityList.get(position);
String colour = this.activities.get(activityName);
if (convertView == null) {
// ActivityView is a LinearLayout with CheckBox, Label and a Button
view = new ActivityView(context, activityName, colour);
} else {
view = (ActivityView) convertView;
view.setActivityName(activityName);
}
return view;
}
}
private class MultiSelectionListener implements MultiChoiceModeListener {
// implementation
}
}
المحلول
You need to use an ActionMode.Callback
. You should read through these docs, it's actually pretty simple to use.
نصائح أخرى
You should check if nothing in your list has an onLongClickListener on it. I had the same issue as you and I realized the long click was called by my ViewHolder so the MultiChoiceListener was never called.
I had a similar issue in which even though the items listed in the ListView
had a CheckBox
, checking them would not invoke the MultiChoiceModeListener
. Through some research and a look at the sample called CustomChoiceList
(which you can import in Android Studio from File
->Import Sample
), I found out that the bound View
s to the ListView
must implement the interface Checkable
. Directly quoting from the CustomChoiceList sample
When a ListView has a choiceMode set, it will allow users to "choose" one or more items. The framework provides default list item layouts that show standard radio buttons or check boxes next to a single line of text:
android.R.layout.simple_list_item_single_choice and android.R.layout.simple_list_item_multiple_choice.
In some cases, you may want to customize this layout. When doing so, the root view must implement the Checkable interface.
I know that the docs could be a bit more clear about the need for implementing the Checkable interface. It says though in the last paragraph that
When a user selects the checkbox, you can invoke the contextual action mode by setting the respective list item to the checked state with setItemChecked().
You don't have to worry about ActionMode.Callback
because it is handled by the MultipleChoiceModeListener
Probably there is bug in your implementation of MultiChoiceModeListener. It works fine for me.