2012-11-21 36 views
5

我有幀佈局:通行證點擊事件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp" > 

<LinearLayout 
    android:id="@+id/widgetView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:layout_margin="10dp" 
    android:gravity="center" > 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/widgetOverlayFrame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="false" > 
</LinearLayout> 

首先佈局包含小窗口的佈局,以及第二個是透明和接收onLongClick用於拖動method.That作品,問題是當我想與窗口小部件進行交互時,點擊某件事件,點擊事件由overlayFrame拍攝。我怎樣才能通過點擊事件槽overlayFrame widgetView?

編輯:

現在我想GestureLister重視overlayFrame的LinearLayout,以某種方式看到MotionEvent代表單一的點擊和長按之間的差異。我面臨的問題是手勢監聽器中的OnLongPress總是被稱爲無論我做什麼,單擊或長按。

對此行爲有解釋嗎? TNX!

回答

0

而不是使用GestureListener可以覆蓋onTouchListener這樣的。

這將調用長按當計時器運行過程,如果向上進來之間,將取消長按

CountDownTimer c; 
    long time=5000; 
    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     switch (ev.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       c= new CountDownTimer(5000, 1000) { 
          public void onTick(long millisUntilFinished) { 
            time=millisUntilFinished; 
          } 

          public void onFinish() { 
           Log.i("","LONG PRESS"); 
          }}.start(); 

      break; 
      case MotionEvent.ACTION_UP: 
       if (time>0) { 
        Log.i("example","short click"); 
        c.cancel(); 
       } 
      break; 
     } 

     return true; 
    } 
+0

好吧,這適用於完美檢測...我現在如何將點擊傳遞到底層? – Veljko

+0

我有這個工作如何... tnx的想法! – Veljko

0

也許設置覆蓋的屬性android:focusable="false"android:focusableInTouchMode="false"就足夠了。

您也可以製作自己的疊加窗口小部件並覆蓋其onInterceptTouchEvent方法以始終返回false。

public class NotTouchableLinearLayout extends LinearLayout { 

    // Override constructors 
    // ... 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     return false; 
    } 
} 
+0

我都試過,沒什麼到目前爲止的工作。任何其他想法? – Veljko

+0

嘗試覆蓋onTouch而不是onInterceptTouchEvent。 –

+0

呵呵,我已經這樣做了,但現在onLongClick overlayLayout不被調用。而且我不能拖動項目。 – Veljko

0

而是在頂部的佈局設置GestureListener,你應該創建自己的類,它擴展LinearLayout並覆蓋它的onTouchEvent方法。 在那裏,你可以實現長按邏輯\短按一下等

單擊事件會先被髮送到窗口小部件的佈局,且僅當它不處理他們(因此它的onTouchEvent回報false),你會讓他們在頂部佈局。

編輯:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_margin="10dp" > 

<LinearLayout 
android:id="@+id/widgetView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="center" 
android:layout_margin="10dp" 
android:gravity="center" > 
</LinearLayout> 

<com.example.touchtesting.MyLinearLayout 
android:id="@+id/widgetOverlayFrame" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clickable="false" > 
</com.example.touchtesting.MyLinearLayout> 

</FrameLayout> 

變化com.example.touchtesting到你的包的名稱。 這是類:

 public class MyLinearLayout extends LinearLayout{ 

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

     private long startClick = 0; 

     @Override 
     public boolean onTouchEvent(MotionEvent ev) { 
      switch (ev.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        startClick = ev.getEventTime(); 
        break; 
       case MotionEvent.ACTION_CANCEL: 
       case MotionEvent.ACTION_UP: 
        if (ev.getEventTime() - startClick < 500) { 
         Log.i("example","short click"); 
        } 
        else { 
         Log.i("example","long click"); 
        } 
        break; 
      } 

      return true; 
     } 

    } 
+0

你能提供一些代碼嗎? – Veljko

+0

編輯了答案 – Rotem

+0

我現在就試試這個;)tnx ......它讓人感覺......希望這個作品......我已經失去了一個這樣的日子。 – Veljko

0

要獲得長遠點擊穿過widgetView LinearLayout添加android:longClickable="true"。 (對於普通點擊添加android:clickable="true"

所以widgetView變爲:

<LinearLayout 
    android:id="@+id/widgetView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:layout_margin="10dp" 
    android:gravity="center" 
    android:longClickable="true"> 
</LinearLayout>