0

我使用 GridView 和通用图像加载器(UIL) 来显示几个图像。我还实现了http://developer.android.com/guide/topics/ui/menus.html#CAB中给出的选择示例(在 ListView 或 GridView 中启用批处理上下文操作)。上下文操作栏显示得很好,我可以更新标题等,但激活的背景指示器会丢失。

当我通过长按选择一个项目时,该项目最初突出显示。然后整个 GridView 重新加载并且指示器丢失。我添加到选择中的所有其他项目也不会突出显示。我不知道 #1 为什么我的画廊会重新加载或 #2 为什么没有显示指标。

有任何想法吗?这是我的代码的一部分(几乎是示例中的存根):

画廊片段:

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) {

    }
});

网格项的 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 ...

适配器

public class ImageAdapter extends BaseAdapter {
    ...
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        ImageLoader.getInstance().displayImage(...);
    }
}

当我调用ActionMode.finish()或关闭CAB时,画廊也会重新加载。


// 编辑:当我删除对 UIL 的 displayImage() 的调用时,我可以很好地选择项目(即查看选择 - 选择本身确实可以工作)。但是,当 CAB 被绘制或移除时,整个东西会重新加载并且选择指示器消失。除了上面的代码,我的画廊片段看起来很像这样

//edit2:并没有解决它(因为我已经在我的代码中拥有它)。

4

2 回答 2

0

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 :)

于 2015-04-22T15:21:56.993 回答
0

您的代表列表项的类需要一个字段来指示该项目已被选中:

 boolean checked;

 public void setChecked(boolean checked) {
   this.checked = checked;
 }

 public booelan isChecked() {
   return this.checked;
 }

然后像这样更新 CAB 回调:

@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean checked) {
     actionMode.setTitle(""+grid.getCheckedItemCount());       
    ((YourItemType)yourGridAdapter.getItemAtPosition(position)).setChecked(checked);
     yourGridAdapter.notifyDataSetChanged();
}

@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
    actionMode.getMenuInflater().inflate(R.menu.image_detail, menu);
    yourGridAdapter.setSelectionMode(true);
    yourGridAdapter.notifyDataSetChanged();
    return true;
}

@Override
public void onDestroyActionMode(ActionMode actionMode) {
    yourGridAdapter.setSelectionMode(false);
    yourGridAdapter.clearAllCheckedItems();
    yourGridAdapter.notifyDataSetChanged();
}

并在您的网格适配器中添加以下内容:

public void setSelectionMode(boolean selectionOn ){
    this.selectionOn = selectionOn ;
}

public void clearAllCheckedItems() {
    for(YourItemType item: yourItemsList) {
        item.setChecked(false);
     }
}

添加getView()这个:

convertView.setBackground(yourItemTypeInstance.isChecked() && selectionOn ? yourDrawableIndicatingThatItemIsChecked : yourDefaultListItemDrawableIndicator )

于 2015-04-17T16:33:50.437 回答