我正在设计一个Android应用程序,我想找到一种方法来处理多个嵌套视图之间的多点触控。
我的布局如下:
Relative layout
|
|---Linear layout
| \---Linear layout
| \--Button A
|
|---Linear layout
\---Button B
这两个按钮大约占屏幕尺寸的50%。我希望能够一个接一个地按下两个按钮。我可以按下按钮B然后按A但是当我按下A时,我无法按下B.
如果我们考虑Android处理事件的方式,这似乎是正常行为。我搜索了我的问题,我只找到了涉及重写自定义视图和“DispatchTouchEvent”方法的复杂解决方案。
你知道是否有一种简单的方法来避免这种行为(能够按A然后再按B和B然后按A)?
我写了一个最小的例子:使用以下布局运行活动
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:splitMotionEvents="true"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="epl.groupe16.testbutton.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="New Button"
android:id="@+id/button" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:weightSum="2">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Button"
android:id="@+id/button21"
/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Button"
android:id="@+id/button20"
android:layout_weight="1"
android:visibility="invisible" />
</LinearLayout>
我可以按下最低按钮,按住我的触摸然后单击上方按钮,但如果我先尝试最高按钮,则最低点不可点击。
提前谢谢
答案 0 :(得分:0)
最后我们发现没有简单的解决方法。 但是,如果有人遇到同样的问题,我想分享一些解决方法。
首先,有一种手动方式:通过创建自定义视图并覆盖方法&#34; DispatchTouchEvent&#34;来重新调整传播事件的正确方法。它很长但可以解决问题。
此外,Google在API 23中添加了PercentRelativeLayout,因此您可以将所有组件放在一个视图中。但是,我们的目标是API 19,Android Marshmallow并不代表当前设备的很大一部分。
最后,我们决定编写自定义SurfaceView和touchEvent侦听器。我们在SurfaceView中绘制了组件并手动处理了clics。
我希望它会对你有所帮助:)。