【问题标题】:How to receive drag events on a top-layer View, but pass through clicks?如何在顶层视图上接收拖动事件,但通过点击?
【发布时间】:2018-08-31 12:49:19
【问题描述】:

该应用有一些接收点击的 UI,例如一个 EditText 接收焦点,以及一个触发动作的按钮(不在下面的屏幕截图中)。但是整个 UI 部分也需要滑动到一侧以显示下面的视图。我将它实现为一个包含 UI 并在拖动时移动(可见)的 View,以及一个不可见但收集拖动输入的顶层 View。

下面的代码部分工作是在拖动不可见视图时可见视图拖动,并且它可以感知拖动和单击之间的区别,但是尽管在 View.OnTouchListener.onTouch() 中返回 false,但下面的视图不可见的可拖动视图没有收到点击。

如何在不影响正常拖动功能的情况下将点击传递到下方的视图?

import android.view.MotionEvent
import android.view.View

class ViewHorizontalSlider(dragView: View,
                           private val moveView: View,
                           private val distancePx: Int = 100) {

    private var xPos = 0f
    private var downX = 0f
    private var isOnClick = false
    private val scrollThreshold = 6

    init {
         dragView.setOnTouchListener { _, event ->
            when (event.actionMasked) {
                MotionEvent.ACTION_DOWN -> {
                    downX = event.x
                    xPos = moveView.x - event.rawX
                    isOnClick = true
                    return@setOnTouchListener true
                }
                MotionEvent.ACTION_MOVE -> {
                    var newX = event.rawX + xPos
                    if (newX > distancePx) newX = distancePx.toFloat()
                    if (newX < 0) newX = 0f
                    moveView.x = newX

                    val movement = Math.abs(downX - event.x)
                    if (isOnClick && (movement > scrollThreshold)) {
                        isOnClick = false;
                    }
                    return@setOnTouchListener true
                }
                MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                    if (isOnClick) {
                        return@setOnTouchListener false
                    } else {
                        return@setOnTouchListener true
                    }
                } else -> {
                    return@setOnTouchListener false
            }
            }
        }
    }

在屏幕截图中,“P1”等是 EditText 视图。整个彩色面板应该可以向右拖动(如 P1,以显示删除 UI),但也应该可以单击(在不可见的覆盖可拖动视图上)但将焦点放在下面的 EditText 上。我希望在上面的代码中只返回 false,但这似乎不足以让点击通过。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    >

    <ImageView
        android:id="@+id/delete"
        android:layout_width="@dimen/perspective_delete_slide"
        android:layout_height="match_parent"
        android:src="@drawable/ic_delete"
        android:scaleType="center"
        android:background="@color/black_light"
        android:contentDescription="@string/delete_perspective"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:focusableInTouchMode="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_marginStart="72dp"
        >

        <EditText
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@null"
            android:inputType="text|textCapSentences"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="12dp"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/marker"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

        <View
            android:id="@+id/marker"
            android:layout_width="8dp"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/name"
            app:layout_constraintTop_toTopOf="parent"
            />

        <View
            android:id="@+id/draggable"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:clickable="false"
            android:focusable="false"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 我尝试在下面的移动视图和上面的可拖动视图之间创建另一个中间视图,当检测到点击时,使用 x/y 创建一个 MotionEvent,并将其分派给点击捕捉器在中间查看,但这似乎也不起作用。
  • 试图找出同样的事情。

标签: android android-touch-event


【解决方案1】:

如果你想要滑动菜单,那么你需要这个Swipe on menu 或类似的Swipe on Menu 2

【讨论】:

  • UI 没有使用 RecyclerView,我们不想使用库来执行此操作,这是非常具体的功能。请注意,它是滑动显示,而不是滑动删除 - 滑动仅限于显示删除选项,然后需要单击删除按钮才能删除。
  • 我正在寻找对我发布的代码的反馈,而不是可能满足或不满足要求的第三方库(尤其是允许点击可拖动的 UI 部分)。
  • 我看不到修复代码的方法,AndroidSwipeLayout 实际上非常适合,并且可以处理传递点击。谢谢。
猜你喜欢
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多