Android ListView项不更改背景

Android ListView项不更改背景,android,listview,Android,Listview,我试图在按下listview项目时更改其背景,尽管尝试了其他SO帖子中提到的内容,但单击该项目时拒绝更改背景 我做错了什么 编辑:我想要实现的目标: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

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

我做错了什么


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

<?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>
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);
    }
}

这是默认listview在单击某个项目时的反应方式(谈论背景更改),但由于我使用自定义列表元素布局,列表根本不响应单击,而且非常单调,因此每当我单击列表元素时,我都会尝试实现背景更改的类似行为

我有一个简单的选择器:

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>
   <?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>
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);
    }
}
公共类TsClickListener实现AdapterView.OnItemClickListener{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
view.setSelected(true);
}
}

Logcat为空(无错误)

尝试添加属性
android:state\u 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>

问题归根结底是根本没有触发
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>