我有一组卡(CardStackAdapter),当用户单击主机片段中的按钮而不是适配器本身的一部分时,我想翻转卡。我一直在尝试使用回调来实现这一点,但目前,如果我手动单击卡将其翻转,则回调仅连接到正确的项目,只有完成此操作后,我才能从活动中的按钮控制翻转动画。因此,只有在按钮被翻转后,我的按钮才能翻转它,使其变得毫无用处和多余。
class CardStackAdapter(private val activity: Activity) :
RecyclerView.Adapter<CardStackAdapter.ViewHolder>() {
private val cardList = ArrayList<Card>()
private var callback: AdapterCallback? = null
fun addItem(item: Card, adapterCallback: AdapterCallback) {
cardList.add(item)
notifyDataSetChanged()
callback = adapterCallback
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(position: Int) {
with(itemView) {
callback?.onClickCallback(card, itemView, position) //When I try this connects it connects to the item who is not on the screen yet
cvParent.setOnClickListener { v ->
flipAnimation(v, card)
callback?.onClickCallback(card, itemView, position)
}
}
}
}
interface AdapterCallback {
fun onClickCallback(itemModel: Card, itemView: View, position: Int)
}
}
这是我的片段:
class CardDetailFragment : Fragment(), CardStackListener, CardStackAdapter.AdapterCallback {
override fun onClickCallback(itemModel: Card, itemView: View, position: Int) {
stringTest = itemModel.cardQuestion
this.itemModel = itemModel
this.itemView = itemView
this.position = position
}
override fun onCardDisappeared(view: View?, position: Int) {}
override fun onCardDragging(direction: Direction?, ratio: Float) {}
override fun onCardSwiped(direction: Direction?) {
// itemView?.tvCardTitle?.performClick()
// adapter.flipAnimation(this.itemView!!, this.itemModel!!)
}
override fun onCardCanceled() {}
override fun onCardAppeared(view: View?, position: Int) {
onClickCallback(itemModel!!, itemView!!, position)
}
override fun onCardRewound() {}
非常感谢。