ScrollView 内的可点击 LinearLayout 不会触发 onClick
Clickable LinearLayout inside ScrollView doesn't fire onClick
我有以下问题。在我的布局中,我有一个带有 fillViewport="true"
的 ScrollView。在里面,我有一个可点击的 LinearLayout(里面有 4 个视图)。看来我的ScrollView拦截了LinearLayout的onClick。我的代码是这样的:
<ScrollView
android:id="@+id/signup_scrollview"
android:layout_marginTop="56dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false"
android:fillViewport="true">
....
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar_actionbar">
....
<LinearLayout
android:id="@+id/add_product_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal">
....
</LinearLayout>
</RelativeLayout>
</ScrollView>
我尝试实现一个 onInterceptTouchEvent(MotionEvent e)
;在我的 ScrollView 上,但无济于事。有人可以指导我如何触发 LinearLayout 的 onClick 吗?谢谢
您禁用了根标签中的点击,参见<ScrollView>
中的android:clickable="false"
,这就是不允许您LinearLayout
可点击。
从 <ScrollView>
中删除 android:clickable="false"
将 android:clickable="true"
添加到 LinearLayout
和
layout.setClickable(true);
在 java 文件中。
把这个写在线性布局标签里面
android:onClick="onClick"
并在您的 java 代码中
public void onClick(View v) {
if(v.getId()==R.id.add_product_button){
// do your code here
System.out.println("Layout click");
}
}
我有以下问题。在我的布局中,我有一个带有 fillViewport="true"
的 ScrollView。在里面,我有一个可点击的 LinearLayout(里面有 4 个视图)。看来我的ScrollView拦截了LinearLayout的onClick。我的代码是这样的:
<ScrollView
android:id="@+id/signup_scrollview"
android:layout_marginTop="56dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false"
android:fillViewport="true">
....
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar_actionbar">
....
<LinearLayout
android:id="@+id/add_product_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal">
....
</LinearLayout>
</RelativeLayout>
</ScrollView>
我尝试实现一个 onInterceptTouchEvent(MotionEvent e)
;在我的 ScrollView 上,但无济于事。有人可以指导我如何触发 LinearLayout 的 onClick 吗?谢谢
您禁用了根标签中的点击,参见<ScrollView>
中的android:clickable="false"
,这就是不允许您LinearLayout
可点击。
从 <ScrollView>
android:clickable="false"
将 android:clickable="true"
添加到 LinearLayout
和
layout.setClickable(true);
在 java 文件中。
把这个写在线性布局标签里面
android:onClick="onClick"
并在您的 java 代码中
public void onClick(View v) {
if(v.getId()==R.id.add_product_button){
// do your code here
System.out.println("Layout click");
}
}