简体   繁体   English

如何在 RecyclerView 中处理 onTouchListener

[英]How to handle onTouchListener inside a RecyclerView

I need some help how to solve my problem.我需要一些帮助来解决我的问题。 I have a recyclerview and its items contain an ImageView. I wanna move imageview with my fingers.我有一个 recyclerview,它的项目包含一个 ImageView。我想用手指移动 imageview。 I create a touchListener and set it in the adapter for the imageview.我创建了一个 touchListener 并将其设置在 imageview 的适配器中。

The following image show how my item looks like.下图显示了我的物品的外观。 I want to move the black square left or right and when it hits the edge of green/blue layout just sets back to the center.我想向左或向右移动黑色方块,当它碰到绿色/蓝色布局的边缘时,它就会回到中心。

I share my adapter code, my listener.我分享我的适配器代码,我的听众。

在此处输入图像描述

Adapter:适配器:

class RecyclerViewAdapter( private val items: List<String>) :
    RecyclerView.Adapter<RecyclerViewAdapter.Holder>() {
    
    inner class Holder(val binding: ItemBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val binding = ItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return Holder(binding)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.binding.image.setOnTouchListener(
            MyTouchInterface(
                image = holder.binding.image,
                container = holder.binding.container
            )
        )
    }

    override fun getItemCount() = items.size
}

TouchListener:触摸监听器:

class MyTouchInterface(
    private val image: AppCompatImageView,
    private val container: View
) : View.OnTouchListener {
    private var downPT = PointF()
    private var startPT = PointF()

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouch(view: View?, event: MotionEvent?): Boolean {
        val defaultPosition =
            container.x + (container.width / 2 - image.width / 2)
        when (event!!.action) {
            MotionEvent.ACTION_MOVE -> {
                image.x = (startPT.x + event.x - downPT.x)
                startPT[image.x] = image.y
                container.doOnLayout {
                    if ((startPT.x + event.x) - downPT.x < (defaultPosition - image.width)) {
                        Log.e("POS left", "${(startPT.x + event.x) - downPT.x}")
                    }

                    if ((startPT.x + event.x) - downPT.x > (defaultPosition + image.width)) {
                        Log.e("POS right", "${(startPT.x + event.x) - downPT.x}")
                    }
                }
            }
            MotionEvent.ACTION_DOWN -> {
                downPT[event.x] = event.y
                startPT[image.x] = image.y
            }
            MotionEvent.ACTION_UP -> {
                image.x = defaultPosition
            }
            else -> {}
        }
        return true
    }
}

The problem is the image doesn't move or just a litle but never going back to the center.问题是图像没有移动或只是一点点但永远不会回到中心。 I've read about it and I found out it is because the recyclerview.我读过它,我发现这是因为 recyclerview。

Thank you for the help!感谢您的帮助!

You must handle suppressLayout of recyclerView while you touch.您必须在触摸时处理 recyclerView 的 suppressLayout。

1 - Create an interface 1 - 创建接口

interface AdapterTouchListener {
    fun onTouched()
    fun onReleased()
}

2 - Your adapter class 2 - 您的适配器 class

class MyAdapter(
    private val touchListener: AdapterTouchListener
) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

// TODO: rest of the adapter code

override fun onBindViewHolder(holder: PlayBackKeyAdapter.MyViewHolder, position: Int) {
    holder.binding.imageKey.setOnTouchListener { _, motionEvent ->
        when (motionEvent.action) {
            MotionEvent.ACTION_DOWN -> {
                touchListener.onTouched()
            }
            MotionEvent.ACTION_UP -> {
                touchListener.onReleased()
            }
        }
        true
    }
}

} }

3 - initialize adapter in your activity/fragment and use suppressLayout 3 - 在您的活动/片段中初始化适配器并使用 suppressLayout

val myAdapter = MyAdapter(object 
 :PlayBackKeyAdapter.AdapterTouchListener{
        override fun onTouched() {
            binding.recyclerView.suppressLayout(true)
        }

        override fun onReleased() {
            binding.recyclerView.suppressLayout(false)
        }

    })

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

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