My fragment contains a viewpager, which contains a listView
This listView rows display a thumbnail on the left and some text on the right. Like the gmail app, when I press on the thumbnail I want it to switch to the actionmode. When pressing elsewhere it opens a different activity related to the specific row pressed on. This part works fine.
In my adapter I have the following code
viewHolder.getImage().setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
documentFragments.setActionMode(true);
}
});
then in my fragment I have the following code
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void setActionMode(final boolean enabled) {
if (enabled && this.actionModeEnabled) {
return;
}
if (!enabled && !this.actionModeEnabled) {
return;
}
this.currentAdapter.setActionModeEnabled(enabled);
if (enabled) {
this.listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
this.listView.startActionMode(new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
SOME CODE
}
@Override
public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
mode.setTitle(getActivity().getString(R.string.selectDocuments));
getActivity().getMenuInflater().inflate(R.menu.documents_action_menu, menu);
// force the checkbox display
DocumentsFragment.this.currentAdapter.notifyDataSetChanged();
return true;
}
@Override
public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
SOME CODE
}
});
} else {
this.listView.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
}
this.actionModeEnabled = enabled;
}
So when I press on the thumbnail everything works fine, I display a checkbox on the row and the actionMode option menu is displayed on the appcompat I'm using. Now what I want is press anywhere on the listview row to select/unselect rows. That's where it doesn't work.
Here's my code:
private void initControls() {
this.listView = (ListView) this.view.findViewById(android.R.id.list);
registerForContextMenu(this.listView);
this.listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
if (this.actionModeEnabled) {
final boolean isChecked = this.listView.isItemChecked(position);
DO SOMETHING
} else {
DO SOMETHING
}
}
When not in actionMode onItemClick is called, but once in actionMode it's not called anymore...
How can I receive the information that a row has been clicked on when in action mode ?
I replaced the CheckBox in the row layout with a CheckedTextView and everything works fine now.