使用 background_selector 不会更改 RecyclerView 视图
RecyclerView view doesn't change using background_selector
我正在尝试在选择项目时切换项目背景颜色。
我可以看到它的逻辑是有效的,但背景不会改变。
日志打印显示选择确实有效,但背景无效。
来自 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)
}
感谢您的帮助。
现在工作。
将其移动到 relative_layout header: 而不是 cardview_layout header
<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">
我正在尝试在选择项目时切换项目背景颜色。
我可以看到它的逻辑是有效的,但背景不会改变。 日志打印显示选择确实有效,但背景无效。
来自 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)
}
感谢您的帮助。
现在工作。 将其移动到 relative_layout header: 而不是 cardview_layout header
<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">