我使用GridView和Universal Image Loader(UIL)来显示几个图像。我还在http://developer.android.com/guide/topics/ui/menus.html#CAB中实现了 Selection 的示例(在ListView或GridView中启用批处理上下文操作)。 上下文操作栏显示得很好,我可以更新标题等,但 activatedBackgroundIndicator 会丢失。
当我通过longpress选择项目时,该项目会初始突出显示。然后整个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时,整个事件会重新加载并且选择指示器消失。 除了上面的代码,我的Gallery Fragment看起来非常像this
// edit2:This没有修复它(因为我已经在代码中使用了它)。
答案 0 :(得分: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 )
答案 1 :(得分:0)
实际上一切正常但是由于加载的ImageView填充了整个区域,它只是覆盖了背景指示。
我&#34;修复&#34;通过在我的GridView项目中添加另一个布局作为选择指标。在onItemCheckedStateChanged()
内,我根据checked
设置了可见性。
我希望这个答案可以帮助那些和我一样的人:)