【问题标题】:ListView item not changing backgroundListView 项目不改变背景
【发布时间】:2015-05-27 20:21:05
【问题描述】:

我正在尝试在按下列表视图项时更改它的背景,尽管尝试了其他 SO 帖子中提到的内容,但该项目在单击时拒绝更改背景。

我做错了什么?


编辑:我想要实现的目标:

这是默认列表视图在单击项目时的反应方式(谈论背景更改),但由于我使用自定义列表元素布局,因此列表根本不响应点击并且非常沉闷,所以我'每当我单击列表元素时,我都会尝试通过改变背景来实现类似的行为。

我有这个简单的选择器:

selector.xml

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

selected_state.xml

   <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/darkblue" />
    </shape>

我尝试将其设置为列表视图的选择器,并使列表视图成为单选

主布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="100dp">
...
                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">

                    <ListView
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:id="@+id/listViewCentral"
                            android:choiceMode="singleChoice"
                            android:listSelector="@drawable/selector" />
                </LinearLayout>
 ...

</RelativeLayout>

列表视图项:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="100dp"
    android:background="@drawable/selector">

...

</RelativeLayout>

我使用OnItemClick监听器将状态设置为选中

主要活动:

ListView list = (ListView)findViewById(R.id.listViewCentral);
        list.setAdapter(stringAdapter);
        list.setOnItemClickListener(new TsClickListener());

听众:

public class TsClickListener implements AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        view.setSelected(true);
    }
}

Logcat 为空(无错误)

【问题讨论】:

    标签: android listview


    【解决方案1】:

    尝试添加属性android:state_focused

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

    【讨论】:

      【解决方案2】:

      问题毕竟是OnItemClick 事件根本没有被触发,解决方案是将'clickable' 属性添加到我的列表视图元素布局的RelativeLayout 并将其设置为true,那么一切正常。

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="100dp"
          android:clickable="true" <-- Problem
          android:background="@drawable/selector">
      
      ...
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-20
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多