gpt4 book ai didi

Android - ListView 项目在选择时更改背景(可检查)

转载 作者:行者123 更新时间:2023-11-29 15:44:27 25 4
gpt4 key购买 nike

我怎样才能让 ListView 项目在按下时改变它的背景,就像它被选中但没有复选框,我知道它有一个 checkListener 或其他东西但我不知道不知道如何申请。按下后保持其颜色背景,再次按下时变回其第一个背景。

最佳答案

你的listItem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
</LinearLayout>

您的选择器:listview_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">

<item android:drawable="@drawable/list_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selected" android:state_activated="true"/>
<item android:drawable="@drawable/list_default"/>

</selector>

你的 listivew 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listViewMainFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/listview_selector.xml"
android:choiceMode="singleChoice">
</ListView>

</LinearLayout>

关于Android - ListView 项目在选择时更改背景(可检查),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35132744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com