自定义列表视图与上下文动作栏选择的项目上下文、自定义、视图、动作

2023-09-05 05:40:29 作者:夜久泪长

我最近开始使用的Andr​​oid 其他动作和的上下文动作条(CAB)。

I recently started using android actionbars and contextual action bars (CAB).

我只有一个活动是一个ListActivity。基本上我用下面的code剪断,以激活CAB:

I have just one activity which is a ListActivity. Basically I use the following code snipped to "activate" the CAB:

ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                      long id, boolean checked) {
        // Here you can do something when items are selected/de-selected,
        // such as update the title in the CAB
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // Respond to clicks on the actions in the CAB
        switch (item.getItemId()) {
            case R.id.menu_delete:
                deleteSelectedItems();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the menu for the CAB
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // Here you can make any necessary updates to the activity when
        // the CAB is removed. By default, selected items are deselected/unchecked.
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // Here you can perform updates to the CAB due to
        // an invalidate() request
        return false;
    }
});

列表的布局:

The layout of the list:

<ImageView
    android:id="@+id/message_on_clipboard_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:minWidth="30dp"
    android:padding="7sp"
    android:src="@android:drawable/presence_online" >
</ImageView>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listitem_background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="5sp"
        android:fadingEdge="horizontal"
        android:singleLine="true"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceLarge" >
    </TextView>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/listitem_background"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/message_length"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5sp"
            android:text="@string/message_length"
            android:textAppearance="?android:attr/textAppearanceSmall" >
        </TextView>

        <TextView
            android:id="@+id/message_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5sp"
            android:text="TextView"
            android:textAppearance="?android:attr/textAppearanceSmall" >
        </TextView>

        <TextView
            android:id="@+id/date_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5sp"
            android:text="@string/date_label" >
        </TextView>

        <TextView
            android:id="@+id/date_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" >
        </TextView>
    </LinearLayout>
</LinearLayout>

和中main.xml中:

And within main.xml:

<ListView
    android:id="@+android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>
</ListView>

现在,如果我做一个长按一个列表项的CAB显示为预期的:

Now if I do a long click on a list item the CAB shows up as expected:

我使用的是MultiChoiceModeListener但遗憾的是选择列表中的项目不改变背景就像这里的例子(浅蓝色背景的项目被选中后):

I use a MultiChoiceModeListener but unfortunately the selected list items do not change the background like in the example here (light blue background after an item is selected):

我必须使用自定义的选择?或者是有一个标准的程序是如何的Andr​​oid不会处理这个,我只是需要让我LinearLayouts透明?我也试过以下,但没有成功:

Do I have to use a custom selector? Or is there a standard procedure how android does handle this and I just need to make my LinearLayouts transparent? I also tried the following but with no success:

ListView项目背景通过自定义选择

这将是巨大的,如果有人可以点我在正确的方向。请让我知道如果你需要更多的应用程序code或XML文件。

It would be great If somebody could point me in the right direction. Please let me know if you need more application code or xml files.

推荐答案

我只测试过这CHOICE_MODE_SINGLE,但在这种情况下,它的工作原理是执行以下操作。

I've only ever tested this in CHOICE_MODE_SINGLE, but in that situation it works by doing the following.

当你选择一个列表项,在code,称之为setItemChecked(位置,选中)的方法(在ListView控件实例)在列表中的项目。

When you select a list item, in code, call "setItemChecked(position, checked)" method (on the ListView instance) for that item in the list.

添加这对XML个别的ListView项目: 安卓背景=机器人:ATTR / activatedBackgroundIndicator

Add this to the XML for individual ListView items: android:background="?android:attr/activatedBackgroundIndicator"