【问题标题】:RecyclerView view doesn't change using background_selectorRecyclerView 视图不会使用 background_selector 改变
【发布时间】:2021-12-25 19:30:02
【问题描述】:

我正在尝试在选择项目时切换项目背景颜色。

我可以看到它的逻辑是有效的,但背景不会改变。 日志打印显示选择确实有效,但背景无效。

来自 background_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/holo_green_light" android:state_pressed="false" android:state_selected="true" />
    <item android:drawable="@android:color/holo_purple" android:state_selected="false" />
</selector>

来自 task_row.xml:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="3dp"
    app:cardElevation="3dp"
    android:id="@+id/tasksCardView"
    android:background="@drawable/background_selector"
    app:cardUseCompatPadding="true">

来自适配器代码:


class MyTasksAdapter (
    val arrayList: ArrayList<Task>,
   
    val selectedItemsList: ArrayList<Task>,
    val deletedItemsList: ArrayList<String>) :
    RecyclerView.Adapter<MyTasksAdapter.ViewHolder>() {

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        private var cardView: CardView = itemView.tasksCardView

        private val selectedItems = SparseBooleanArray()
        override fun onClick(view: View) {
            if (selectedItems[adapterPosition, false]) {
                selectedItems.delete(adapterPosition)
                cardView.isSelected = false
                Log.d("Item selected AA", "position: $position")
            }
            else {
                selectedItems.put(adapterPosition, true)
                view.isSelected = true
                Log.d("Item selected BB", "position: $position")
            }
        }

        init {
            itemView.setOnClickListener(this)

        }

也尝试将 cardview 更改为 view,但也没有用。


class MyTasksAdapter (
    val arrayList: ArrayList<Task>,
    val selectedItemsList: ArrayList<Task>,
    val deletedItemsList: ArrayList<String>) :
    RecyclerView.Adapter<MyTasksAdapter.ViewHolder>() {

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        //private lateinit var item: ClipData.Item
        private var cardView: CardView = itemView.tasksCardView

        private val selectedItems = SparseBooleanArray()
        override fun onClick(view: View) {
            if (selectedItems[adapterPosition, false]) {
                selectedItems.delete(adapterPosition)
                view.isSelected = false
                Log.d("Item selected AA", "position: $position")
            }
            else {
                selectedItems.put(adapterPosition, true)
                view.isSelected = true
                Log.d("Item selected BB", "position: $position")
            }
        }

        init {
            itemView.setOnClickListener(this)

        }

感谢您的帮助。

【问题讨论】:

  • 尝试使用前景而不是背景。
  • foreground 确实现在改变了项目的颜色,如预期的那样(切换),但我看不到项目内部的任何东西:-(
  • 使用alpha属性,这样颜色就不会完全隐藏内容。

标签: android-recyclerview background


【解决方案1】:

现在工作。 将其移至 relative_layout 标头:而不是 cardview_layout 标头

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="3dp"
    app:cardElevation="3dp"
    android:id="@+id/tasksCardView"
    android:hapticFeedbackEnabled="true"
    app:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        **android:background="@drawable/background_selector"**
        android:padding="16dp">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2013-03-30
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多