【问题标题】:onTouchListener to RecyclerView have disabled its scrollingonTouchListener 到 RecyclerView 已禁用其滚动
【发布时间】:2018-11-02 18:17:52
【问题描述】:

我有一个可水平滚动的 recyclerView,当在任何地方触摸 recyclerView 时,应该会发生下拉菜单。我尝试了很多方法,例如 onclick,但没有奏效。唯一有效的是 onTouchListener,但在实现后滚动不起作用。

这是recyclerView的代码

<android.support.v7.widget.RecyclerView
        android:id="@+id/rv_selected_groups"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="16dp"
        android:clickable="true"
        android:layout_marginTop="0dp"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/iv_dropdown"
        app:layout_constraintTop_toBottomOf="@id/tv_select_group"
        android:focusable="true" />

这里是使用的 onTouchListener 的代码

 mRecyclerViewSeletedGroups.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
        when (motionEvent.action){
            MotionEvent.ACTION_UP -> {
                dropdownFlag = !dropdownFlag
                mPresenterImpl.onDropdownClicked(dropdownFlag)
            }
        }
        return@OnTouchListener true
    })

所以当我尝试 onclick 而不是 onTouch 时,它不起作用。

我想实现 onTouch 以及滚动应该可以工作。

我该如何解决这个问题?

【问题讨论】:

  • 把你的 onTouch 逻辑放在 recyclerview 项目中。那应该可以解决您的问题。

标签: android xml kotlin


【解决方案1】:

问题出在你的:

return@OnTouchListener true

如果你总是返回 true 这意味着:

如果监听器已经消费了事件则为真,否则为假。

如果你总是消费这个事件,你的滚动将不起作用。您需要检查您的逻辑以仅在需要时返回 true

【讨论】:

  • 兄弟刚刚将return语句设置为false并且有效,非常感谢您的帮助兄弟
  • 很高兴能帮上忙 ;)
猜你喜欢
  • 2020-11-10
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
相关资源
最近更新 更多