【问题标题】:Click on view behind button if it's not clickable (isClickable = false)如果按钮不可点击,请点击按钮后面的视图(isClickable = false)
【发布时间】:2018-04-05 20:42:45
【问题描述】:

我有一个带有一排按钮的回收站。 这些按钮仅用于样式目的并设置为 isClickable = false。 按钮布置在网格视图中,以便动态处理任意数量的按钮。 当我单击行上的任意位置时,我想触发在每个行父视图上设置的 onClickListener。

我尝试在整个视图上设置一个空的相对视图并在其上设置 onClickListener。没有结果。

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_base_row_dark"
    android:minHeight="@dimen/grid_10x"
    android:stateListAnimator="@drawable/selector_elevation_button">

    <RelativeLayout
        android:id="@+id/answerContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="@dimen/grid_8x"
        android:layout_marginStart="@dimen/grid_3x"
        android:layout_marginTop="@dimen/grid_1x">

<android.support.v7.widget.RecyclerView
            android:id="@+id/gridAnswerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/answerQuestionTitleTextView"
            android:layout_marginEnd="@dimen/grid_2x"
            android:layout_marginTop="@dimen/grid_1x"
            android:layout_marginBottom="@dimen/grid_1x"
            android:columnWidth="120dp"
            android:gravity="center"
            android:horizontalSpacing="@dimen/grid_1x"
            android:visibility="gone"
            tool:visibility="visible" />

    </RelativeLayout>

   <RelativeLayout
        android:id="@+id/answerRowClickableArea"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

在适配器中:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {

answerRowClickableArea.setOnClickListener(goToIndex(rowPosition))

}

我还尝试在我的切换按钮类中覆盖 GestureDetector.OnGestureListener,告诉按钮仅在不可点击时才使用触摸事件。 我的意图是,如果按钮不使用触摸事件,它将通过 onClickListener 传递给父级。

class OvalToggleButton(context: Context, attributeSet: AttributeSet) : ToggleButton(context, attributeSet), GestureDetector.OnGestureListener {
    override fun onShowPress(e: MotionEvent?) {
    }

    override fun onSingleTapUp(e: MotionEvent?): Boolean {
        return !isClickable
    }

    override fun onDown(e: MotionEvent?): Boolean {
        return false
    }

    override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
        return true
    }

    override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {
        return true
    }

    override fun onLongPress(e: MotionEvent?) {
    }
 }

还是没有结果。

有没有一种简单的方法可以“忽略”视图并触摸背后的内容?

【问题讨论】:

    标签: java android android-layout kotlin ontouchlistener


    【解决方案1】:

    您是否尝试将行的布局设置为可点击?

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setClickable(true);
    

    【讨论】:

    • 是的,但我觉得行布局位于底层,因此按钮会消耗触摸。
    • 实际上我已经使用了可点击的线性布局,在其上使用了一个按钮,但在 recyclerView 中没有。您仍然可以在按钮上使用setEnable() 方法,但不确定。
    • 好的,我明白了。我有一些可能导致问题的因素。 1. 我使用的是切换按钮 2. 我在回收站中有一个回收站
    【解决方案2】:

    我通过将 answerRowClickableArea 放在相同的相对布局内并将每个边缘与我的网格视图对齐来使其工作。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tool="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_base_row_dark"
        android:minHeight="@dimen/grid_10x"
        android:stateListAnimator="@drawable/selector_elevation_button">
    
        <RelativeLayout
            android:id="@+id/answerContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="@dimen/grid_8x"
            android:layout_marginStart="@dimen/grid_3x"
            android:layout_marginTop="@dimen/grid_1x">
    
        <android.support.v7.widget.RecyclerView
                    android:id="@+id/gridAnswerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/answerQuestionTitleTextView"
                    android:layout_marginEnd="@dimen/grid_2x"
                    android:layout_marginTop="@dimen/grid_1x"
                    android:layout_marginBottom="@dimen/grid_1x"
                    android:columnWidth="120dp"
                    android:gravity="center"
                    android:horizontalSpacing="@dimen/grid_1x"
                    android:visibility="gone"
                    tool:visibility="visible" />
    
                <ImageView
                    android:id="@+id/answerRowClickableArea"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignTop="@id/gridAnswerView"
                    android:layout_alignStart="@id/gridAnswerView"
                    android:layout_alignEnd="@id/gridAnswerView"
                    android:layout_alignBottom="@id/gridAnswerView" />
    
            </RelativeLayout> 
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多