android - Android ListView 项目选择器不起作用
问题描述
我正在尝试使用选择器文件为我的简单 arrayadapter 列表视图创建一个设计,但它不起作用。我错过了什么吗?
我的列表视图
<ListView
android:id="@+id/docTypeListView"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="140dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imgActiveSec"
app:layout_constraintTop_toTopOf="parent"
android:background="@android:color/transparent"
android:dividerHeight="0dp"
android:listSelector="@drawable/list_color_selector" />
list_color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Normal state. -->
<item android:state_pressed="false" android:state_selected="false"
android:alpha="0.4" android:drawable="@android:color/transparent"
android:textColor="#FF0000" android:textStyle="bold" />
<!-- pressed state. -->
<item android:drawable="@android:color/transparent" android:state_pressed="true" />
<!-- Selected state. -->
<item android:alpha="0.8" android:drawable="@android:color/transparent"
android:state_pressed="false" android:state_selected="true" android:textColor="#228B22" />
</selector>
适配器设计
val adapter = ArrayAdapter<String>(activity, R.layout.simple_listview, docTypes)
docTypesListView.adapter = adapter
simple_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:textSize="12dp"
android:textColor="@drawable/list_item_text_selector"
android:padding="10dp"
android:gravity="left|center_vertical"
android:background="@android:color/transparent"
tools:text="Item_1" />
list_item_text_selector.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/color_white" android:state_pressed="true" android:textStyle="bold" />
<item android:color="@color/color_white" android:state_focused="true" android:textStyle="bold" />
<item android:color="@color/color_white" />
</selector>
我的预期设计如下图所示
护照是我的选择
解决方案
你的代码有很多错误,所以我从头开始重新做来帮助你。试试我下面的选择器工作代码Recyclerview
。
需要注意的每个状态在您的选择器中都是白色的,因此它在屏幕上不可见
在您的应用程序级别
build.gradle
添加实现'androidx.recyclerview:recyclerview:1.0.0'
在
MainActivity.kt
添加
private lateinit var linearLayoutManager: LinearLayoutManager
private lateinit var myAdapter: RcvAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listItems = listOf("IDENTITY CARD", "PASSPORT", "DRIVER'S LICENSE")
linearLayoutManager = LinearLayoutManager(this)
sample_RCV.layoutManager = linearLayoutManager
myAdapter = RcvAdapter()
sample_RCV.adapter = myAdapter
myAdapter.updateList(listItems)
}
}
- 在
activity_main.xml
添加
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/sample_RCV"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
- 创建
RcvAdapter.kt
和添加
class RcvAdapter : RecyclerView.Adapter<RcvAdapter.Holder>() {
private var documentsList: List<String>? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
return Holder(LayoutInflater.from(parent.context).inflate(R.layout.recv_layout, parent, false))
}
fun updateList(mList: List<String>) {
this.documentsList = mList
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: Holder, position: Int) {
holder.updateUi(documentsList!![position])
}
override fun getItemCount(): Int {
return documentsList!!.size
}
inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private var my_tv: TextView = itemView.findViewById(R.id.my_tv)
fun updateUi(text: String) {
my_tv.text = text
}
}
}
- 创建
recv_layout.xml
和添加
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/my_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_text_selector"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:padding="10dp"
android:text="Item_1"
android:textSize="12sp" />
</LinearLayout>
- 最后,在
drawable
创建list_item_text_selector.xml
和添加中。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/color_white" android:state_activated="true"/>
<item android:drawable="@color/color_white" android:state_pressed="true"/>
<item android:drawable="@color/color_white" android:state_checked="true"/>
<item android:drawable="@color/color_white" android:state_focused="true"/>
<item android:drawable="@color/color_white_gray"/>
</selector>
- 在
colors.xml
添加
<color name="color_white">#FFFFFF</color>
<color name="color_white_gray">#808080</color>
希望这可以帮助。
推荐阅读
- android - 如何在扩展 AlertDialog 时修复“必须在添加内容之前调用 requestFeature()”
- sql - 使用 SQL 用另一个表的 PK 值填充字段
- node.js - 环回 3 中数据源的配置环境问题
- javascript - HTML元素内的增量变量
- powershell - Powershell CSV文件导入行为奇怪
- c - Fortran 和 C 互操作性:从 c 接收返回值(从 Fortran 调用)
- javascript - 连接地图搜索框
- php - PHP 7.2.14 “short_open_tag = On”被忽略
- python - cleverhans 中的哪个攻击是灰盒攻击,cleverhans 库中哪个是白/黑盒攻击?
- wordpress - 如何在 XAMPP-VM (Mac OS) 上安装 WordPress 主题或插件