【发布时间】: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