简体   繁体   English

如何将点击监听器添加到轮播项目

[英]How to add click listener to Carousel item

I've created a Carousel using MotionLayout by following this guide: https://developer.android.com/training/constraint-layout/motionlayout/carousel我按照本指南使用 MotionLayout 创建了一个 Carousel: https://developer.android.com/training/constraint-layout/motionlayout/carousel

I've gotten everything from this guide working - I can swipe through a carousel of items.我已经从本指南中获得了一切 - 我可以在项目轮播中滑动。

However, I'm having trouble adding an onClickListener to my carousel's items.但是,我在将 onClickListener 添加到轮播项目时遇到了问题。 I've tried setting the onClickListener in the adapter like so:我试过像这样在适配器中设置 onClickListener:

carousel.setAdapter(object : Carousel.Adapter {
  override fun populate(view: View?, index: Int) {
    view.setOnClickListener {
      // do something
    }
  }
})

However, this makes the carousel unresponsive to swiping.但是,这会使轮播对滑动没有反应。 When I try to swipe the carousel it executes my onClick function but doesn't execute my onSwipe transition.当我尝试滑动轮播时,它会执行我的 onClick function 但不会执行我的 onSwipe 转换。

My question is, what is the proper way to set an onClick function for a Carousel item?我的问题是,为轮播项目设置 onClick function 的正确方法是什么?

While searching for an answer, I've seen a few posts that reference Carousel.setOnItemClickListener() .在寻找答案时,我看到了一些引用Carousel.setOnItemClickListener()的帖子。 This is exactly what I need, but it seems to have been deprecated.这正是我所需要的,但它似乎已被弃用。

I've also seen posts to say to override onTouchListener and run your onClick code on the ACTION_UP event.我还看到帖子说要覆盖 onTouchListener 并在 ACTION_UP 事件上运行您的 onClick 代码。 I've tried a couple variations of this code snippet, but haven't been able to get it to work:我已经尝试了这个代码片段的几个变体,但一直无法让它工作:

carousel.setAdapter(object : Carousel.Adapter {
  override fun populate(view: View?, index: Int) {
    view.setOnTouchListener { view, motionEvent ->
      if(motionEvent.action == MotionEvent.ACTION_UP) {
        // do something
      }
      view.performClick()
    }
  }
})

Would really appreciate any help!非常感谢任何帮助!

No better solution没有更好的解决方案

You could have override parent MotionLayout class like this.Intercept the slide event您可以像这样覆盖父 MotionLayout class。拦截幻灯片事件

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.constraintlayout.motion.widget.MotionLayout
import kotlin.math.abs

/**
 * @author : litao
 * @email  : onresume@live.com
 * @date   : 2022/5/31 5:31 下午
 */
class TestMotionLayout constructor(
    context: Context,
    attrs: AttributeSet?,
    defStyleAttr: Int,
) : MotionLayout(context, attrs, defStyleAttr) {

    private var mInitX = 0f
    private var mInitY = 0f

    private var mTouchSlop = 10

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {

        when (ev.actionMasked) {
            MotionEvent.ACTION_DOWN -> {
                mInitX = ev.x
                mInitY = ev.y
            }
            MotionEvent.ACTION_MOVE -> {
                val moveX = abs(x - mInitX)
                val moveY = abs(y - mInitY)

                if (moveX > mTouchSlop || moveY > mTouchSlop){
                 
                    val obtain = MotionEvent.obtain(ev)
                    obtain.action = MotionEvent.ACTION_DOWN
                    dispatchTouchEvent(obtain)
                    onTouchEvent(obtain)
                    return true
                }
            }
            MotionEvent.ACTION_UP -> {
            }
        }
        return false
    }


}

Worked for me为我工作

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

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