Android 从片段列表视图中的复选框启用上下文菜单

Android 从片段列表视图中的复选框启用上下文菜单,android,android-fragments,android-listview,Android,Android Fragments,Android Listview,我有一个片段,其中嵌入了一个列表视图和一些其他的视图s。ListView正在为其列表项使用自定义布局,请参见下文 编辑:这就是我想要实现的。从文档中: 在某些情况下,上下文操作 如果提供常用操作项,则可能需要添加复选框或 类似的UI元素,允许用户选择项目,因为 可能无法发现长时间单击行为。当用户选择 复选框,则可以通过设置 使用setItemChecked()将各个列表项设置为选中状态 CustomListViewItem.xml: <?xml version="1.0" encoding

我有一个
片段
,其中嵌入了一个
列表视图
和一些其他的
视图
s。
ListView
正在为其列表项使用自定义布局,请参见下文

编辑:这就是我想要实现的。从文档中:

在某些情况下,上下文操作 如果提供常用操作项,则可能需要添加复选框或 类似的UI元素,允许用户选择项目,因为 可能无法发现长时间单击行为。当用户选择 复选框,则可以通过设置 使用setItemChecked()将各个列表项设置为选中状态

CustomListViewItem.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_margin="@dimen/activity_vertical_margin"
    android:layout_width="match_parent"
    android:weightSum="10"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/tvNotesListAdapterTitle" />

        <TextView
            android:textStyle="italic"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/tvNotesListAdapterTimestamp" />
    </LinearLayout>

    <RelativeLayout

        android:layout_weight="9"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <CheckBox
            android:id="@+id/cb_notes"
            android:button="@null"
            android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
            android:drawableEnd="?android:attr/listChoiceIndicatorMultiple"
            android:layout_centerVertical="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </RelativeLayout>


</LinearLayout>

我还没有找到我上面描述的任何工作示例。在此问题上的任何帮助都将不胜感激。简言之,当选中一个或多个
复选框时,我不确定在何处以及如何调用上下文菜单。

通过在我的
ArrayAdapter
类中实现回调接口解决了这一问题

private NotesAdapterCallback callback;

public interface NotesAdapterCallback {
    public void checkBoxChecked();
}

public void setCallback(NotesAdapterCallback callback) {
    this.callback = callback;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        row = inflater.inflate(
                R.layout.arrayadapter_notes, null);
    } else {
        row = convertView;
    }

    TextView tvTitle = (TextView) row.findViewById(R.id.tvNotesListAdapterTitle);
    TextView tvTimeStamp = (TextView) row.findViewById(R.id.tvNotesListAdapterTimestamp);
    CheckBox cbNote = (CheckBox) row.findViewById(R.id.cb_notes);

    String timeStamp = allNotes.get(position).getTimestamp();
    String note = allNotes.get(position).getNote();

    tvTitle.setText(note);
    tvTimeStamp.setText(timeStamp);

    cbNote.setOnCheckedChangeListener(this);

    return row;

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        if (callback != null) {
            callback.checkBoxChecked();
        }
    }
}
private ActionMode mActionMode;

@Override
public void checkBoxChecked() {
    if (mActionMode != null) {
        return;
    }else{
        mActionMode = getActivity().startActionMode(mActionModeCallback);
    }
}

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

    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
        return true;
    }

    // Called each time the action mode is shown. Always called after onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.context_delete:

                mode.finish(); // Action picked, so close the CAB
                return true;
            case R.id.context_move:

                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
    }
};
在我的
片段中实现接口,如下所示

private NotesAdapterCallback callback;

public interface NotesAdapterCallback {
    public void checkBoxChecked();
}

public void setCallback(NotesAdapterCallback callback) {
    this.callback = callback;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        row = inflater.inflate(
                R.layout.arrayadapter_notes, null);
    } else {
        row = convertView;
    }

    TextView tvTitle = (TextView) row.findViewById(R.id.tvNotesListAdapterTitle);
    TextView tvTimeStamp = (TextView) row.findViewById(R.id.tvNotesListAdapterTimestamp);
    CheckBox cbNote = (CheckBox) row.findViewById(R.id.cb_notes);

    String timeStamp = allNotes.get(position).getTimestamp();
    String note = allNotes.get(position).getNote();

    tvTitle.setText(note);
    tvTimeStamp.setText(timeStamp);

    cbNote.setOnCheckedChangeListener(this);

    return row;

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        if (callback != null) {
            callback.checkBoxChecked();
        }
    }
}
private ActionMode mActionMode;

@Override
public void checkBoxChecked() {
    if (mActionMode != null) {
        return;
    }else{
        mActionMode = getActivity().startActionMode(mActionModeCallback);
    }
}

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

    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
        return true;
    }

    // Called each time the action mode is shown. Always called after onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.context_delete:

                mode.finish(); // Action picked, so close the CAB
                return true;
            case R.id.context_move:

                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
    }
};
不要忘记在
ArrayAdapter上设置回调

notesAdapter = new NotesArrayAdapter(getActivity(), allNotes);
lwNotes.setAdapter(notesAdapter);
notesAdapter.setCallback(this);