1

我有一个 recyclerview.widget.ListAdapter 在网格中显示一些卡片。当我点击一张卡片时,它会来到网格的前面,翻转,当你看到前面时,你应该会喜欢它。我在卡片正面的角落有一个 ImageView,它应该在喜欢与否之间切换,显示我已经定义的不同的可绘制对象。

我的 ViewHolder 看起来像这样:

class GameCardViewHolder(private val binding : ItemGameCardBinding) :
        RecyclerView.ViewHolder(binding.root){

    companion object {
        fun from(parent: ViewGroup): GameCardViewHolder {
            val layoutInflater = LayoutInflater.from(parent.context)
            val binding = ItemGameCardBinding.inflate(layoutInflater, parent, false)
            return GameCardViewHolder(binding)
        }
        ...
    }

    fun bind(productCard: ProductCard, listener: OnClickListener){
        binding.productCard = productCard
        binding.executePendingBindings()
        ...  
    }
}

我的 item_game_card.xml 看起来像这样:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="productCard"
            type="company.app.model.ProductCard" />
    </data>
    <androidx.constraintlayout.widget.ConstraintLayout>
    ...
        <ImageView
            android:id="@+id/gameItemLikeImageView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            ...
            app:imageDrawable="@{productCard.liked}"
            android:onClick="@{() -> productCard.toggleLike()}"
        />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

我的 BindingAdapters.kt:

@BindingAdapter("imageDrawable")
fun bindImageDrawable(imgView: ImageView, boolean: LiveData<Boolean>?) {
    Timber.d("Binding liked: $boolean")
    when(boolean?.value){
        true -> imgView.setImageResource(R.drawable.ic_card_like)
        false ->  imgView.setImageResource(R.drawable.ic_card_dislike)
    }
}

还有我的 ProductCard.kt:

data class ProductCard(
    val position: Int,
    val product: Product
){
    ...
    private val _liked = MutableLiveData(false)
    val liked : LiveData<Boolean>
        get() = _liked

    fun toggleLikeProduct(){
        Timber.d("Toggle like before: ${_liked.value}")
        val oldValue = _liked.value!!
        _liked.value = !oldValue
        Timber.d("Toggle like after: ${_liked.value}")
    }
}

当我点击 ImageView 时,控制台每次都正确打印出前后值,所以我知道likedLiveData 的值实际上是在变化的,但是 View 没有相应地更新,那是因为绑定适配器只是被调用一次(binding.executePendingBindings()我猜是当 RecyclerView 绑定 ViewHolder 时,而不是每次 LiveData 更改时)。

我希望每次likedLiveData 更改时都运行 BindingAdapter 代码。

当我使用 ViewModel 和 Fragment 布局执行此操作时,每次 ViewModel 中的 LiveData 更改时,View 都会更改,因为 xml 正在引用该 LiveData,但我不知道为什么,当涉及到 ProductCard 和 recyclerview 项目布局时,即使xml 引用的是 LiveData,View 没有改变,xml 实际上并没有绑定到 LiveData,这是拥有 LiveData 的全部意义。

我已经用一种非常粗鲁、不雅的方式实现了这种行为,在该视图上设置了一个 onClickListener,并强制它在 OnBindViewHolder 上更改它的资源可绘制,如下所示:

fun bind(productCard: ProductCard, listener: OnClickListener){
    binding.productCard = productCard
    binding.executePendingBindings()
    binding.gameItemLikeImageView.setOnClickListener {
        it?.let {
            val card = binding.productCard
            Timber.d("Tried changing liked status: ${card!!.liked}")
            val old = card.liked
            card.liked = !old
            binding.productCard = card
            val card2 = binding.productCard
            Timber.d("Tried changing liked status: ${card2!!.liked}")
            if (!old){
                it.setBackgroundResource(R.drawable.ic_card_like)
            }
            else {
                it.setBackgroundResource(R.drawable.ic_card_dislike)
            }
        }
    }
    ...
}

但我真的希望每张卡片的行为都由它所绑定的 ProductCard 对象的状态来控制。

我也试过binding.executePendingBindings()每次点击都强制打电话,但也没有用。

如何让 item_game_card.xml 观察 LiveData 的变化?

4

1 回答 1

0

app:imageDrawable不会接受LiveData<Boolean>,我想知道为什么不接受LiveData<Product>?尝试像这样绑定初始值:

app:imageDrawable="@{product.liked ? R.drawable_liked : R.drawable_neutral}"

对于需要设置的绑定LiveDataLifecycleOwner

class ProductsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        // Inflate view and obtain an instance of the binding class.
        val binding: ProductsBinding = DataBindingUtil.setContentView(this, R.layout.products)

        // Specify the current activity as the lifecycle owner.
        binding.setLifecycleOwner(this)
    }
}

请参阅文档

于 2019-10-25T01:21:01.140 回答