首页 > 解决方案 > 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>

我的预期设计如下图所示

护照是我的选择

在此处输入图像描述

标签: androidlistviewkotlin

解决方案


你的代码有很多错误,所以我从头开始重新做来帮助你。试试我下面的选择器工作代码Recyclerview

需要注意的每个状态在您的选择器中都是白色的,因此它在屏幕上不可见

  1. 在您的应用程序级别build.gradle添加

    实现'androidx.recyclerview:recyclerview:1.0.0'

  2. 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)
    }
}
  1. 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>
  1. 创建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
            }
        }
    }
  1. 创建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>
  1. 最后,在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>
  1. colors.xml添加

<color name="color_white">#FFFFFF</color>
    <color name="color_white_gray">#808080</color>

希望这可以帮助。


推荐阅读