【问题标题】:Item click listener not working on a Auto Scrolling Recyclerview项目单击侦听器不适用于自动滚动 Recyclerview
【发布时间】:2019-05-09 08:33:40
【问题描述】:

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

private final Runnable SCROLLING_RUNNABLE = new Runnable() {

    @Override
    public void run() {
        mRecyclerView_news.smoothScrollBy(pixelsToMove, 0);
        mHandler.postDelayed(this, duration);
    }
};

private void scrollRV(){
    final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

    mRecyclerView_news.setLayoutManager(layoutManager);           
    mRecyclerView_news.setHasFixedSize(true);
    mAdapter = new NewsAdapter(MainActivity.this, newsList, this);
    mRecyclerView_news.setAdapter(mAdapter);

    mHandler.postDelayed(SCROLLING_RUNNABLE, 500);
}

我也在实现一个 Click 监听器,但是当 Recyclerview 滚动时,没有一个项目被点击。当 recyclerview 不滚动时,点击监听器工作正常。

我该如何克服这个问题?

【问题讨论】:

  • 你在哪里调用scrollRV方法?

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


【解决方案1】:

我有一个类似的问题,是因为我有一个回收器视图和一个滚动视图。因为它们都是垂直滚动,所以它影响了我的 OnCLickListener。嵌套滚动视图或完全取出滚动视图对我有用。希望这可以帮助!

【讨论】:

  • 我只有一个recyclerview。没有附加滚动视图。
【解决方案2】:

我遇到了类似的问题,我通过这种方式解决了

将这些接口和类写入您的活动文件中

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) {

                        }
                    }))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多