I use a GridView and Universal Image Loader (UIL) to display a couple images. I've also implemented the example for Selection given in http://developer.android.com/guide/topics/ui/menus.html#CAB (Enabling batch contextual actions in a ListView or GridView). The Contextual Action Bar gets displayed well and I can update the title etc. but the activatedBackgroundIndicator gets lost.
When I select an item via longpress the item is initally highlighted. Then the whole GridView reloads and the indicator is lost. Every other item I add to the selection will also not be highlighted. I've no idea #1 why my gallery reloads or #2 why the indicator isn't shown.
Any ideas? Here's part of my code (which is pretty much the stub from examples):
Gallery Fragment:
grid.setLongClickable(true);
grid.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
grid.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked) {
actionMode.setTitle(""+grid.getCheckedItemCount());
}
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
actionMode.getMenuInflater().inflate(R.menu.image_detail, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
Grid Item's XML:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dip"
android:background="?android:attr/activatedBackgroundIndicator"
>
<ImageView ...
Adapter
public class ImageAdapter extends BaseAdapter {
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
ImageLoader.getInstance().displayImage(...);
}
}
The gallery also reloads when I call ActionMode.finish() or dismiss the CAB.
//edit: When I remove the call to UIL's displayImage() I can select items just fine (i. e. see the selection - selection itself does work either ways). But when the CAB is drawn or removed the whole thing reloads and the selection indicator vanishes. Aside from the above code my Gallery Fragment looks pretty much like this
//edit2: This did not fix it (as I already have it in my code).
Actually everything works fine but since the ImageView that's loaded fills the whole area it simply overlays the background indicator.
I "fixed" that by adding another Layout to my GridView items that acts as a selection indicator. Within onItemCheckedStateChanged()
I set the visibility according to checked
.
I hope this answer will help anyone who's just as stu*** as I was :)