'Why is selecting first view in RecyclerView affecting last view, and vice versa?
I have been working my way through Google's Android Kotlin basics Tutorials. Most recently I have been making a project a little outside the scope of the tasks and have been trying to create collapsing CardViews within a RecyclerView. The two problem I am having is that when I select the first or last CardView, or try to expand the first or last, they affect one another. They also leave white space between each other when they are collapsed the size of the collapsing section. I have been searching for an answers for a while now and have found nothing. I attached code for the card layout and the Adapter I am using. Any feedback would be appreciated.
Card Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.card.MaterialCardView
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:cardCornerRadius="16dp"
app:cardElevation="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/add_button"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/ic_add"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:id="@+id/check_button"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="@drawable/ic_checked"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_marginTop="12dp"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_height="150dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/image"
tools:text="Amphibian Name"/>
<ImageView
android:id="@+id/dropdown_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:src="@drawable/ic_dropdown"
app:layout_constraintTop_toTopOf="@id/name"
app:layout_constraintBottom_toBottomOf="@id/name"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/dropdown_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="This will be the type"/>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="12dp"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@id/type"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="This will be the description of the selected Amphibian"/>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="16dp"
app:layout_constraintTop_toBottomOf="@id/description"
app:layout_constraintStart_toStartOf="parent"
tools:text="cost"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</FrameLayout>
Adapter
class AmphibianAdapter(private val context: Context) :
ListAdapter<Amphibian, AmphibianAdapter.AmphibianViewHolder>(DiffCallBack) {
class AmphibianViewHolder(private val layout: View): RecyclerView.ViewHolder(layout) {
private val photo: ImageView = layout.findViewById(R.id.image)
private val title: TextView = layout.findViewById(R.id.name)
val type: TextView = layout.findViewById(R.id.type)
private val description: TextView = layout.findViewById(R.id.description)
private val cost: TextView = layout.findViewById(R.id.cost)
val dropdownButton: ImageView = layout.findViewById(R.id.dropdown_button)
val addButton: ImageView = layout.findViewById(R.id.add_button)
val checkButton: ImageView = layout.findViewById(R.id.check_button)
val dropdown: View = layout.findViewById(R.id.dropdown)
var cardView: CardView = layout.findViewById(R.id.card)
fun bind(amphibian: Amphibian) {
photo.setImageResource(amphibian.photo)
title.text = amphibian.info.name
type.text = amphibian.info.type
description.text = amphibian.info.description
cost.text = amphibian.cost.toString()
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AmphibianViewHolder {
val layout = LayoutInflater.from(parent.context)
.inflate(R.layout.amphibian_card_layout, parent, false)
return AmphibianViewHolder(layout)
}
override fun onBindViewHolder(holder: AmphibianViewHolder, position: Int) {
val current = getItem(position)
holder.bind(current)
holder.dropdownButton.setOnClickListener{
if(holder.dropdown.visibility == View.GONE) {
TransitionManager.beginDelayedTransition(holder.cardView, AutoTransition())
holder.dropdown.visibility = View.VISIBLE
holder.dropdownButton.setImageResource(R.drawable.ic_up_button)
} else {
TransitionManager.beginDelayedTransition(holder.cardView, AutoTransition())
holder.dropdown.visibility = View.GONE
}
}
holder.addButton.setOnClickListener {
holder.cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.highlight))
holder.addButton.visibility = View.GONE
holder.checkButton.visibility = View.VISIBLE
}
holder.checkButton.setOnClickListener {
holder.cardView.setCardBackgroundColor(ContextCompat.getColor(context, R.color.transparent))
holder.checkButton.visibility = View.GONE
holder.addButton.visibility = View.VISIBLE
}
}
companion object DiffCallBack : DiffUtil.ItemCallback<Amphibian>() {
override fun areItemsTheSame(oldItem: Amphibian, newItem: Amphibian): Boolean {
return oldItem.info.name == newItem.info.name
}
override fun areContentsTheSame(oldItem: Amphibian, newItem: Amphibian): Boolean {
return oldItem.info.description == newItem.info.description
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
