项目单击侦听器在自动滚动Recyclerview上不起作用

时间:2019-05-09 08:33:40

标签: java android android-recyclerview scroll android-adapter

我正在使用下面的代码自动滚动我的回收站视图。

foo(bar = "baz")

我还实现了Click侦听器,但是在滚动Recyclerview时,没有任何项目被单击。当recyclerview不滚动时,单击侦听器可以正常工作。

我该如何克服这个问题?

2 个答案:

答案 0 :(得分:0)

我有一个类似的问题,该问题是由我拥有回收者视图和滚动视图引起的。由于它们都是垂直滚动,因此影响了我的OnCLickListener。嵌套的滚动视图或仅将滚动视图完全移出对我来说都是有效的。希望这可以帮助!

答案 1 :(得分:0)

我遇到了类似的问题,并以此方式解决了

在您的活动文件中写入这些界面和课程

interface ClickListener {
    fun onClick(view: View?, position: Int)
    fun onLongClick(view: View?, position: Int)
}



 open class RecyclerTouchListener(
    context: Context?,
    recycleView: RecyclerView,
    val clicklistener: ClickListener?
) :
    OnItemTouchListener {
    private val gestureDetector: GestureDetector
    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
        val child = rv.findChildViewUnder(e.x, e.y)
        if (child != null && clicklistener != null && gestureDetector.onTouchEvent(e)) {
            clicklistener.onClick(child, rv.getChildAdapterPosition(child))
        }
        return false
    }

    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
    override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}

    init {
        gestureDetector = GestureDetector(context, object : SimpleOnGestureListener() {
            override fun onSingleTapUp(e: MotionEvent): Boolean {
                return true
            }

            override fun onLongPress(e: MotionEvent) {
                val child = recycleView.findChildViewUnder(e.x, e.y)
                if (child != null && clicklistener != null) {
                    clicklistener.onLongClick(child, recycleView.getChildAdapterPosition(child))
                }
            }
        })
    }
}

实施回收者视图

 rvAds.addOnItemTouchListener( RecyclerTouchListener(mActivity,rvAds,object :ClickListener{
                        override fun onClick(view: View?, position: Int) {


                            val layout = view!!.mainlayoutAds

                            val intent = Intent(mActivity, AdsDetailsActivity::class.java)
                            intent.putExtra(AdsDetailsActivity.TITLE, list[position].massage)
                            intent.putExtra(AdsDetailsActivity.IMAGE, list[position].image)
                            intent.putExtra(AdsDetailsActivity.MESSAGE, list[position].massage)
                            startActivity(intent)


                            layout.setOnClickListener {



                            }
                        }

                        override fun onLongClick(view: View?, position: Int) {

                        }
                    }))