Android ListView项目选择器不起作用

时间:2019-03-18 05:52:08

标签: android listview kotlin

我正在尝试使用选择器文件为我的简单arrayadapter listview创建一个设计,但是它不起作用。我想念什么吗?

我的列表视图

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

我期望的设计如下图所示

护照是我选择的项目

enter image description here

1 个答案:

答案 0 :(得分:0)

您的代码有很多错误,因此我再次从头开始以帮助您。 使用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>

希望这会有所帮助。