简体   繁体   English

回收商查看物品翻转

[英]Recycler View Item flip

Actually I am using recycler view and adding a layout in the rows and I am using flip animation on cardviews(when clicked on it).实际上我正在使用回收器视图并在行中添加布局,并且我在卡片视图上使用翻转 animation(单击它时)。 The problem is when I add multiple items in the recycler the flip animation works only with the first item.问题是当我在回收站中添加多个项目时,翻转 animation 仅适用于第一个项目。 I used toast to make sure that click function is working with other items or not, turns out it's working but flip animation is not working with any other items.Can any one help me out here我用吐司来确保单击 function 是否与其他项目一起工作,结果它正在工作,但翻转 animation 不与任何其他项目一起工作。任何人都可以在这里帮助我

This is my code这是我的代码

override fun onCardClick(item: PacketModel, position: Int) {


        val scale = this.resources.displayMetrics.density
        frontCard.cameraDistance= 8000 * scale
        backCard.cameraDistance = 8000 * scale




        front_anim = AnimatorInflater.loadAnimator(context, R.animator.front_animator) as AnimatorSet
        back_anim = AnimatorInflater.loadAnimator(context, R.animator.back_animator) as AnimatorSet

        if (isFront){
            front_anim.setTarget(frontCard)
            back_anim.setTarget(backCard)
            front_anim.start()
            back_anim.start()
            isFront = false

        }else
        {
            front_anim.setTarget(backCard)
            back_anim.setTarget(frontCard)
            back_anim.start()
            front_anim.start()
            isFront = true


        }


        Toast.makeText(context, item.Name , Toast.LENGTH_SHORT).show()
    }

}

This is the adapter Class这是适配器 Class

class PacketAdapter (val packetList: ArrayList<PacketModel> , var clickListener2: onPacketItemClickListener): RecyclerView.Adapter<PacketAdapter.ViewHolder>(){



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        val a = LayoutInflater.from(parent?.context).inflate(R.layout.packet, parent, false)


        return ViewHolder(a)


    }

    override fun getItemCount(): Int {


        return packetList.size

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val packet : PacketModel = packetList[position]



        holder.intialize(packet, clickListener2)


    }


    class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView)
    {

        val packetTime = itemView.findViewById<TextView>(R.id.packetTime)
        val timeMessage = itemView.findViewById<TextView>(R.id.timeMessage)


        fun intialize(item: PacketModel, action: onPacketItemClickListener){

            packetTime.text = item.Name
            timeMessage.text = item.Age

            itemView.setOnClickListener {
                action.onCardClick(item, adapterPosition)
            }
        }
    }

    interface onPacketItemClickListener{
        fun onCardClick (item: PacketModel, position: Int)

    }

}

You should place your card flipping code inside your recyclerview adapter so that recyclerview can recycle it as it should be.您应该将卡片翻转代码放在 recyclerview 适配器中,以便 recyclerview 可以按应有的方式回收它。 You can place your card flipping code inside itemview onClicklistener:您可以将卡片翻转代码放在 itemview onClicklistener 中:

 itemView.setOnClickListener {

     // Place your flipping code here

     action.onCardClick(item, adapterPosition)
 }

Remove flipping code from onCardClick callback.从 onCardClick 回调中删除翻转代码。 Let me know if it works fine.让我知道它是否工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM