我有一排带按钮的回收商。 这些按钮仅用于样式设置,并设置为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?) {
}
}
仍然没有结果。
是否有一种简单的方法可以“忽略”视图并触及背后的内容?
答案 0 :(得分:0)
您是否尝试设置行可点击的布局?
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setClickable(true);
答案 1 :(得分:0)
我通过将我的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>