将ListView项设置为选中

时间:2013-08-20 09:56:54

标签: android listview background selector

我有一个主要的详细ListView设置,我想更改主片段中所选项目的背景。

我曾经有过以下代码,但这不允许我使用完整的选择器:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        // Highlight selected item
        if (position == selectedPos) {
            view.setBackgroundColor((view.getResources()
            .getColor(R.color.nw_white)));
        } else {
            view.setBackgroundColor((view.getResources()
            .getColor(R.color.nw_grey)));
        }
        return view;
    }

它工作正常,但我想使用选择器。 (因为我希望项目在焦点和点击时适当地改变背景。)

所以我把它改成了:

            @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        // Highlight selected item
        if (position == selectedPos) {
            view.setSelected(true);
            // view.setBackgroundColor((view.getResources()
            // .getColor(R.color.nw_row_download_master_selected)));
        } else {
            view.setSelected(false);
            // view.setBackgroundColor((view.getResources()
            // .getColor(R.color.nw_row_download_master_normal)));
        }
        Log.d(TAG, "pos " + position + " is selected: "
            + view.isSelected());

        return view;
    }

列表行如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_row_collection_master"
    android:orientation="horizontal"
    >

   ...
</LinearLayout>

选择器是这样的:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/row_master_detail_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/row_master_detail_focused"
          android:state_focused="true" />
    <item android:drawable="@color/nw_row_master_master_selected"
          android:state_selected="true" />
    <item android:drawable="@color/nw_row_master_normal" />
</selector>

该行响应点击事件但未被选中,即使记录器确实选择了视图!发生了什么事?

3 个答案:

答案 0 :(得分:3)

我不知道为什么视图不会显示为已选中,即使它已设置为选中,但此代码应该有效:

使用ListView.setItemChecked()

mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setItemChecked(position, isActivated);

选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/row_master_detail_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/row_master_detail_focused"
          android:state_focused="true" />
    <item android:drawable="@color/nw_row_master_master_selected"
          android:state_activated="true" />
    <item android:drawable="@color/nw_row_master_normal" />
</selector>

由于android:state_activated仅在Honeycomb之后可用,因此请将其添加到getView()

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                // Highlight selected item
                if (position == selectedPos) {
                    view.setBackgroundColor((view.getResources()
                            .getColor(R.color.nw_row_master_selected)));
                } else {
                    Drawable sel = view.getResources().getDrawable(
                            R.drawable.selector_row_collection_master);
                    view.setBackgroundDrawable(sel);
                }
            }

答案 1 :(得分:1)

您必须为selector设置item view并删除selector的{​​{1}}。只需将list-view的{​​{1}}设为list-view即可。希望这会有所帮助。

答案 2 :(得分:1)

您应该使用state_activated,而不是state_selected,请参阅https://stackoverflow.com/a/13634298/1283554

  • 使您的列表可以选择,例如在android:choiceMode="singleChoice"

  • 上设置ListView
  • 使用'state_activated="true"状态的选择器作为项目背景

  • 使用myListView.setItemChecked(position, true)

  • 选择ListView上的项目

注意:state_activated仅在Honeycomb之后可用。对于旧版本,我认为你不能只使用选择器。您可能需要在getView

中设置背景