2

我有這樣一個佈局:onTouch()不會調用父佈局

LinearLayout 
    RelativeLayout(id = test) 
     FrameLayout (From a LIB. id = lib) 

我嘗試添加onTouchListener()test,但它不工作。

我想:

@OnTouch(R.id.test) 
public boolean test(View v,MotionEvent e) { NOT WORK } 

,我嘗試一個簡單的監聽器:

test.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
NOT WORK 
        return false; 
       } 
      }); 

我試圖偵聽器添加到FrameLayout但沒有工作過。

我不想修改庫。 任何人有想法使用OnTouch事件?

<RelativeLayout 
    android:id="@+id/camera_preview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/black" 
    android:layout_weight="1"> 

     <com.flurgle.camerakit.CameraView 
      android:id="@+id/camera" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center_horizontal" 
      android:adjustViewBounds="true" 
      app:ckCropOutput="false" 
      app:ckFacing="back" 
      app:ckFlash="off" 
      app:ckFocus="continuous" 
      app:ckJpegQuality="100" 
      app:ckMethod="standard"/> 

    <TextView 

在CameraView:

focusMarkerLayout.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent motionEvent) { 
      int action = motionEvent.getAction(); 
      if (motionEvent.getAction() == MotionEvent.ACTION_UP && mFocus == CameraKit.Constants.FOCUS_TAP_WITH_MARKER) { 
       focusMarkerLayout.focus(motionEvent.getX(), motionEvent.getY()); 
      } 

      mPreviewImpl.getView().dispatchTouchEvent(motionEvent); 
      return true; 
     } 
    }); 

感謝。


解決方案:


LinearLayout 
    RelativeLayout(id = test) 
     CustomRelativeLayout() 
      FrameLayout (From a LIB. id = lib) 

創建CustomRelativeLayout

public class CustomRelativeLayout extends RelativeLayout{ 
    public CustomRelativeLayout(Context context) { 
     super(context); 
    } 

    public CustomRelativeLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent ev) { 
     return false; 
    } 
} 

問題:

Touch 
-> Activity:dispatchTouchEvent 
-> LinearLayout:dispatchTouchEvent 
-> CustomRelativeLayout:dispatchTouchEvent 
-> CameraView:dispatchTouchEvent (return true) STOPPED everything 

解決方案:

Touch 
-> Activity:dispatchTouchEvent 
-> LinearLayout:dispatchTouchEvent 
-> CustomRelativeLayout:dispatchTouchEvent 
-> CustomRelativeLayout:onTouch 
-> LinearLayout:onTouch 
-> Activity:onTouch (IT WORK :)) 

回答

2

最可能FrameLayout消耗觸摸事件。一旦觸摸事件被任何視圖佔用,該事件將不會被分派給父級。如果FrameLayout有一個附加的點擊監聽器,那麼MotionEvent將不會達到RelativeLayout

你也可以繼承RelativeLayout,覆蓋ViewGroup#dispatchTouchEvent(MotionEvent)和跟蹤將被分派到佈局的孩子(你的情況FrameLayout)的MotionEvent

在這種情況下,您的視圖層次結構將是這樣一個:

<LinearLayout> 
    <com.example.CustomRelativeLayout> 
     <FrameLayout> 
    </com.example.CustomRelativeLayout> 
</LinearLayout> 
+0

我做了我的自定義佈局@覆蓋 公共布爾dispatchTouchEvent(MotionEvent EV){ 返回FALSE; } 但它不能正常工作 – ketom

+0

這隻會幫助你調試'MotionEvent's,看看發生了什麼,這是不應該幫助你在任何方面。另外,返回'false'將不允許將這個'MotionEvent'派發給子代,而是返回'true'來讓'FrameLayout'處理點擊。 – azizbekian

+0

當我觸摸屏幕時,CustomLayouts調度運行。 – ketom

0

我覺得你的RelativeLayout由FrameLyout這就是爲什麼觸摸不工作覆蓋。 發表您的佈局XML代碼

+0

有我的代碼。更新 – ketom