【问题标题】:How to handle onTouchListener inside a RecyclerView如何在 RecyclerView 中处理 onTouchListener
【发布时间】:2023-01-18 19:21:02
【问题描述】:

我需要一些帮助来解决我的问题。我有一个 recyclerview,它的项目包含一个 ImageView。我想用手指移动图像视图。我创建了一个 touchListener 并将其设置在图像视图的适配器中。

下图显示了我的物品的外观。我想向左或向右移动黑色方块,当它碰到绿色/蓝色布局的边缘时,它就会回到中心。

我分享我的适配器代码,我的听众。

适配器:

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
}

触摸监听器:

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
    }
}

问题是图像没有移动或只是一点点但永远不会回到中心。我读过它,我发现这是因为 recyclerview。

感谢您的帮助!

【问题讨论】:

    标签: android android-recyclerview ontouchlistener


    【解决方案1】:

    您必须在触摸时处理 recyclerView 的 suppressLayout。

    1 - 创建接口

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

    2 - 你的适配器类

    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 - 在您的活动/片段中初始化适配器并使用 suppressLayout

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

    【讨论】:

      猜你喜欢
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 2019-12-26
      相关资源
      最近更新 更多