【问题标题】:Multi touch childview not getting events多点触控子视图没有收到事件
【发布时间】:2013-10-31 13:33:39
【问题描述】:

我有一个具有线性布局和一些子视图的活动。如果我用一根手指在子视图外触摸,然后用另一根手指触摸子视图,则不会在子视图上触发触摸事件。如果我直接触摸子视图,我会收到一个事件。

我覆盖了活动中的触摸事件并调用了子视图,这可行,但 X,Y 来自父级,而不是相对于子级。子视图,即框架布局,如何获取所有触摸事件,而不管它之外的其他手指触摸?

 @Override 
 public boolean onTouchEvent(MotionEvent event) {
    if (event != null && clock != null) {
         event.offsetLocation(-frameLayout1.getLeft(),- frameLayout1.getTop());
         clock.onTouchEvent(event);

    }
     return false;
 };

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="fill_parent" android:layout_height="fill_parent">

    <LinearLayout 
        android:id="@+id/LearningLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@+id/frameLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
        </FrameLayout>

        <TextView
            android:id="@+id/targetTime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:gravity="center_horizontal"
            android:text="TextView" android:textSize="64dp" android:layout_gravity="center_horizontal"/>

        <TextView
            android:id="@+id/textualTime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:gravity="center_horizontal"
            android:text="TextView" android:textSize="64dp" android:layout_gravity="center_horizontal"/>


        <TextView
            android:id="@+id/textualTime2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:gravity="center_horizontal"
            android:text="TextView" android:textSize="64dp" android:layout_gravity="center_horizontal"/> 

    </LinearLayout>


    <com.google.ads.AdView android:id="@+id/adView"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             ads:adUnitId="a14e531e444c51a"
                             ads:adSize="SMART_BANNER" android:layout_marginTop="5dp" android:layout_alignParentBottom="true"/>

</RelativeLayout>

【问题讨论】:

  • 为孩子计算 x y 能解决您的问题吗?
  • 我试过使用 event.offsetLocation(-clock.getLeft(),- clock.getHeight()); ,但没有用。有没有比这更干净的方法而不直接调用子视图 onTouchEvent ?
  • 正在寻找 X 和 Y 以确定点击哪个视图??
  • 不,我希望框架中有一些东西可以将触摸事件定向到子视图而无需额外计算。
  • 为什么要像您描述的那样使用多次触摸?这不是意外的行为。这是您的应用需要的吗?

标签: android


【解决方案1】:

基本上,clock.getLeft() 和 clock.getTop() 返回 0 。时钟是框架布局内的视图子类。如果我使用框架布局 getLeft() 和 getTop() 它返回正确的偏移量并且可以工作。 有没有比调用 childview 触摸事件和偏移更好的方法?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多