繁体   English   中英

使用上下文操作栏在自定义列表视图上选择的项目

[英]selected item on custom listview with contextual action bar

我最近开始使用机器人为其他动作上下文的动作条 (CAB)。

我只有一个活动是ListActivity。 基本上我使用以下代码剪切来“激活”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;
    }
});

列表的布局:

<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中:

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

现在,如果我长按一下列表项,CAB就会按预期显示:

出租车

我使用MultiChoiceModeListener但不幸的是,所选列表项不会像这里的示例那样更改背景(选择项目后的浅蓝色背景):

在此输入图像描述

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

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

如果有人能指出我正确的方向,那就太棒了。 如果您需要更多应用程序代码或xml文件,请告诉我。

我只在CHOICE_MODE_SINGLE中对此进行过测试,但在这种情况下,它可以通过执行以下操作来实现。

  • 当您选择列表项时,在代码中,为列表中的该项调用“setItemChecked(position,checked)”方法(在ListView实例上)。

  • 将其添加到单个ListView项的XML中:
    android:background="?android:attr/activatedBackgroundIndicator"

只需在路上创建一个名为custom_background的drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/highlight" android:state_activated="true"/>
    <item android:drawable="@color/normal"/>
</selector>

并在父布局上设置为背景:

android:background="@drawable/custom_background"

只是尝试支持ICS设备,但是这个新的布局可以实现您想要实现的目标,同时保持选定的行突出显示。

adapter = new SimpleCursorAdapter(getActivity(),
     android.R.layout.simple_list_item_activated_1, 
     null, 
     from, 
     to,
     0);

对于仍然遇到此问题的人,请检查您的android:minSdkVersion。 如果它太低,选择器背景颜色可能不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM