使用CAB在自定义ListView中进行多项选择

Kon*_*rer 29 android android-listview

看完并试着好几天后,我放弃了并寻求帮助.

<edit>我正在使用ActionBarSherlock.</ edit>

我想要实现的目标: 一个ListView,每行有一个自定义布局,用户可以在其中选择多个列表项.选定的列表项应具有不同的背景颜色.如果至少选择了一个项目,则应显示上下文操作栏(CAB).它应该或多或少看起来像GMail应用程序中的多个电子邮件选择.唯一的区别是,在gmail应用程序中,通过单击行的复选框来完成选择,而我不希望有一个复选框,但无论用户单击的位置,都应该选择一行. GMail应用程序中的多个选择

我尝试过:本教程中,使用Checkable行布局和一些逻辑来在切换检查状态时更改背景颜色,我得到了一切正常工作,除了我无法在ListView上注册像OnItemClickListener这样的点击监听器来显示CAB .既没有为每一行View提供点击监听器也有帮助,因为这阻止了更改所选项目的背景颜色.我也尝试将MultiChoiceModeListener添加到ListView中

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { //.. });
Run Code Online (Sandbox Code Playgroud)

结果相同,没有背景颜色变化.

我在寻找:提示或教程或示例代码如何做到这一点.如果您需要一些代码片段来提供帮助,请告诉我们.

Luk*_*rog 36

看看代码是否有助于你(它基本上是ListActivity一个自定义适配器来保存已检查项目的状态(+不同的背景)):

public class CABSelection extends ListActivity {

    private ArrayList<String> mItems = new ArrayList<String>();
    private SelectionAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        for (int i = 0; i < 24; i++) {
            mItems.add("Name" + i);
        }
        // R.layout.adapters_cabselection_row is a LinearLayout(with green
        // background(#99cc00)) that wraps an ImageView and a TextView
        mAdapter = new SelectionAdapter(this,
                R.layout.adapters_cabselection_row, R.id.the_text, mItems);
        setListAdapter(mAdapter);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {

            private int nr = 0;

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.cabselection_menu, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                StringBuilder sb = new StringBuilder();
                Set<Integer> positions = mAdapter.getCurrentCheckedPosition();
                for (Integer pos : positions) {
                    sb.append(" " + pos + ",");
                }               
                switch (item.getItemId()) {
                case R.id.edit_entry:
                    Toast.makeText(CABSelection.this, "Edited entries: " + sb.toString(),
                            Toast.LENGTH_SHORT).show();
                    break;
                case R.id.delete_entry:
                    Toast.makeText(CABSelection.this, "Deleted entries : " + sb.toString(),
                            Toast.LENGTH_SHORT).show();
                    break;
                case R.id.finish_it:
                    nr = 0;
                    mAdapter.clearSelection();
                    Toast.makeText(CABSelection.this, "Finish the CAB!",
                            Toast.LENGTH_SHORT).show();
                    mode.finish();
                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                nr = 0;
                mAdapter.clearSelection();
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode,
                    int position, long id, boolean checked) {
                if (checked) {
                    nr++;
                    mAdapter.setNewSelection(position, checked);                    
                } else {
                    nr--;
                    mAdapter.removeSelection(position);                 
                }
                mode.setTitle(nr + " rows selected!");

            }

        });
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        l.setItemChecked(position, !mAdapter.isPositionChecked(position));
    }

    private class SelectionAdapter extends ArrayAdapter<String> {

        private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();

        public SelectionAdapter(Context context, int resource,
                int textViewResourceId, List<String> objects) {
            super(context, resource, textViewResourceId, objects);
        }

        public void setNewSelection(int position, boolean value) {
            mSelection.put(position, value);
            notifyDataSetChanged();
        }

        public boolean isPositionChecked(int position) {
            Boolean result = mSelection.get(position);
            return result == null ? false : result;
        }

        public Set<Integer> getCurrentCheckedPosition() {
            return mSelection.keySet();
        }

        public void removeSelection(int position) {
            mSelection.remove(position);
            notifyDataSetChanged();
        }

        public void clearSelection() {
            mSelection = new HashMap<Integer, Boolean>();
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
            v.setBackgroundColor(Color.parseColor("#99cc00")); //default color
            if (mSelection.get(position) != null) {
                v.setBackgroundColor(Color.RED);// this is a selected position so make it red
            }
            return v;
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

R.layout.adapters_cabselection_row是一个绿色背景的行(一个非常简单的)的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#99cc00" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/the_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="17sp"
        android:textStyle="bold" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

R.menu.cabselection_menu是一个包含3个选项(编辑,删除,完成CAB)的菜单文件,除了弹出一个Toast有关所选行的消息之外,它们不执行任何操作:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/edit_entry"
        android:icon="@android:drawable/ic_menu_edit"
        android:title="Edit!"/>
    <item
        android:id="@+id/delete_entry"
        android:icon="@android:drawable/ic_menu_delete"
        android:title="Delete!"/>
    <item
        android:id="@+id/finish_it"
        android:icon="@android:drawable/ic_menu_crop"
        android:title="Get me out!"/>

</menu>
Run Code Online (Sandbox Code Playgroud)


Sea*_*ins 13

我认为最简单的方法是申请

android:background="android:attr/activatedBackgroundIndicator"

您将要点击的布局.

这会在使用时突出显示布局

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
Run Code Online (Sandbox Code Playgroud)

无论如何,为我工作

  • 因为正确的格式是`?android:attr/activatedBackgroundIndicator`而不是,它不适用于预蜂窝版本,这仅适用于API 11+ (3认同)

Kon*_*rer 11

MultiChoiceModeListener如果要支持API级别<11,则使用ActionBarSherlock 在Luksprog的答案中尚未使用.

解决方法是使用onItemClickListener.

列表设置:

listView = (ListView) timeline.findViewById(android.R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemsCanFocus(false);
listView.setAdapter(new ListAdapter(getActivity(), R.layout.cleaning_list_item, items));
Run Code Online (Sandbox Code Playgroud)

ListFragment或ListActivity的监听器:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    SparseBooleanArray checked = listView.getCheckedItemPositions();
    boolean hasCheckedElement = false;
    for (int i = 0; i < checked.size() && !hasCheckedElement; i++) {
        hasCheckedElement = checked.valueAt(i);
    }

    if (hasCheckedElement) {
        if (mMode == null) {
            mMode = ((SherlockFragmentActivity) getActivity()).startActionMode(new MyActionMode());
            mMode.invalidate();
        } else {
            mMode.invalidate();
        }
    } else {
        if (mMode != null) {
            mMode.finish();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

其中MyActionMode是ActionMode.Callback的实现:

private final class MyActionMode implements ActionMode.Callback { /* ... */ }
Run Code Online (Sandbox Code Playgroud)