0

我使用动作模式显示选择模式,但是当所选项目计数为0时,它将自动退出操作模式。除非用户按返回键,否则我想保持选择模式。

ModeCallback mModeCallBack = new ModeCallback();
mGridview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
mGridview.setMultiChoiceModeListener(mModeCallBack);

private class ModeCallback implements GridView.MultiChoiceModeListener {
...
}
4

3 回答 3

3

试试这个是否能解决你的问题

public void onItemCheckedStateChanged(ActionMode mode, int position,
            long id, boolean checked) {
        if (position == theInitialPosition)
             return; // so we start the action mode but there aren't any items checked
        }
        int selectCount = gridView.getCheckedItemCount();
        if (selectCount > 0) {
            notify = true;              
            dataArray.add(position);
            // if you select another item you'll have two selected items(because of the dummy item) so you need to take care of it 
            switch (selectCount) {
            case 1:
                mode.setSubtitle("One item added ");
                break;
            default:
                mode.setSubtitle("" + selectCount
                        + " items added to favorites");
                break;
            }
        } 

       }

或者...

public boolean onItemLongClick(AdapterView<?> parent, View view,
      int position, long id) {

    if (mActionMode != null) {
      return false;
    }
    selectedItem = position;

    // start the action mode using the ActionMode.Callback defined above
    mActionMode = MyActivity.this
        .startActionMode(mActionModeCallback);
    view.setSelected(true);
    return true;
  }
});
      }

      private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

// called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
  // Inflate a menu resource providing context menu items
  MenuInflater inflater = mode.getMenuInflater();
  // assumes that you have "contexual.xml" menu resources
  inflater.inflate(R.menu.xxxxxxx, menu);
  return true;
}
于 2014-03-20T05:05:23.373 回答
0

我已经解决了这个问题。

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
    ...
    mCheckedCount = mGridview.getCheckedItemCount();
    // if we unselect the last item, cout is 0, we set it to -1, so it will not exit action mode(because if cout==0, mode.finish()).
    // if we select 1 item, we should plus this "1" back. unless the count will be woring.
    if (mCheckedCount == 0) {
        Utils.modifyFileValue(mGridview, "mCheckedItemCount", mCheckedCount + (checked ? 1 : -1));
    }

    String title = "";
    // we must get the count again, because we changed it.
    mCheckedCount = mGridview.getCheckedItemCount();
    title = mContext.getResources().getString(R.string.select_albums, mCheckedCount > 0 ? mCheckedCount : 0);

    mSelectedConvCount.setText(title);

    ....

    if (checked) {
        mSelectedIds.add(selectedId);
    } else {
        mSelectedIds.remove(selectedId);
    }
}

public static void modifyFileValue(Object object, String filedName, int filedValue) {
    //for my case, I want to get mCheckedItemCount in AbsListView.class, but i passed Gridview, so it should getSuperclass(). because GridView don't have mCheckedItemCount.
    Class classType = object.getClass().getSuperclass();
    Field fild = null;
    try {
        fild = classType.getDeclaredField(filedName);
        fild.setAccessible(true);
        fild.set(object, filedValue);
    } catch (NoSuchFieldException ex) {
        ex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
于 2014-03-26T06:06:28.653 回答
0

问题源于 AbsListView.java 中这段不灵活的代码,它在取消选择最后一项时无条件地关闭 ActionMode:

class MultiChoiceModeWrapper implements MultiChoiceModeListener {

    ...

    @Override
    public void onItemCheckedStateChanged(ActionMode mode,
            int position, long id, boolean checked) {
        mWrapped.onItemCheckedStateChanged(mode, position, id, checked);

        // If there are no items selected we no longer need the selection mode.
        if (getCheckedItemCount() == 0) {
            mode.finish();
        }
    }
}

由于 AbsListView 没有提供任何钩子来合法地阻止这种严厉的行为,因此当上面的代码调用它时,您不得不诉诸丑陋的技巧,即从您自己的 AbsListView 子类中从 getCheckedItemCount 返回一个虚假值:

public class ActionModeHackGridView extends GridView {

    ...

    @Override
    public int getCheckedItemCount() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        String name = ActionModeHackGridView.class.getName();
        for (int i = 0; i < stackTrace.length; i++) {

            // First find this method in the stack
            if (stackTrace[i].getClassName().equals(name)
                    && stackTrace[i].getMethodName().equals("getCheckedItemCount")) {

                // ...then examine the caller of this method
                if (stackTrace[i + 1].getClassName().equals(
                        "android.widget.AbsListView$MultiChoiceModeWrapper")
                        && stackTrace[i + 1].getMethodName().equals("onItemCheckedStateChanged")) {

                    // Return a non-zero value so AbsListView doesn't dismiss the ActionMode
                    return 1;
                }
                break;
            }
        }
        return super.getCheckedItemCount();
    }
}

您可以在任何 AbsListView 子类(例如 ListView)中使用相同的 hack。

于 2014-09-08T15:36:36.620 回答