简体   繁体   English

ListView项目不更改背景

[英]ListView item not changing background

I'm trying to change the background of a listview item when it's pressed, and despite having tried what has been mentioned in other SO posts the item refuses to change background when clicked. 我试图在按下列表视图项目时更改其背景,尽管尝试了其他SO帖子中提到的内容,但单击该项目时拒绝更改背景。

What I'm I doing wrong? 我做错了什么?


EDIT: What I want to achieve: 编辑:我想实现:

在此处输入图片说明

This is how the default listview reacts when clicking an item (talking about the background change), but since I'm using custom list elements layout the list doesn't respond to clicks at all and is very dull, so I'm trying to achieve similar behaviour with the background changing whenever I click a list element. 这是单击一个项目时默认listview的反应方式(谈论背景变化),但是由于我使用的是自定义列表元素布局,因此该列表根本不响应点击,而且非常呆板,因此我尝试每当我单击列表元素时,背景都会发生变化,从而实现类似的行为。

I have this simple selector: 我有这个简单的选择器:

selector.xml 选择器.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 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>

I tried setting it to the selector of the listview and making the list view single choice 我尝试将其设置为列表视图的选择器,并使列表视图成为单选

Main layout: 主要布局:

<?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>

List view item: 清单检视项目:

<?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>

And I used OnItemClick listener to set the state to selected 我使用OnItemClick侦听器将状态设置为选中

Main activity: 主要活动:

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

Listener: 听众:

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

Logcat is empty (no errors) Logcat为空(无错误)

try adding the property android:state_focused 尝试添加属性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>

The problem after all was the OnItemClick event wasn't being fired at all, and the solution was to add the ' clickable ' attribute to the RelativeLayout of my list view element's layout and set it to true , everything works fine then. 毕竟问题是根本没有触发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>

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

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