2011-10-21 25 views
1

我有一個對話框來從中選擇文件。該對話框包含一個顯示目錄和文件的列表視圖,並具有用於導航的onItemClickListener。整個對話框的Android onTouchListener

我該如何應對在目錄結構中出現的投擲事件?我試着根RelativeLayout的設置的OnTouchListener到調度觸摸事件對我GestureListener:

final GestureDetector detector = new GestureDetector(new MyGestureDetector(tvCurrentPath));  
     dialog.findViewById(R.id.rlFilelist).setOnTouchListener(new OnTouchListener() { 
      public boolean onTouch(View view, MotionEvent e) { 
       detector.onTouchEvent(e); 
       return false; 
      } 
     }); 

的事件被註冊onTouch(),但onFling()方法從來沒有在MyGestureDetector調用。但是,如果我將OnTouchListener附加到列表視圖,它將按預期工作。這種方法的問題是,listview並不總是充滿數據,當它是空的或者當點擊下面的項目時,不會觸發onTouch()事件。

對話框佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rlFilelist" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:focusable="true" >  

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/llButtons" > 

     <TextView 
      android:id="@+id/tvCurrentPath" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" > 
     </TextView> 

     <ListView 
      android:id="@+id/lvFileListing" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_above="@id/llButtons" 
      android:layout_below="@id/tvCurrentPath" > 
     </ListView> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/llButtons" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="5dip" 
     android:layout_marginTop="5dip" > 

     <Button 
      android:id="@+id/btAddDirectory" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_marginRight="10px" 
      android:text="@string/host_tv_dg_add_directory" /> 

     <Button 
      android:id="@+id/btUp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="10px" 
      android:layout_toRightOf="@id/btAddDirectory" 
      android:text="@string/host_tv_dg_navigate_up" /> 

     <Button 
      android:id="@+id/btClose" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/btUp" 
      android:text="@string/_tv_close" /> 
    </RelativeLayout> 

</RelativeLayout> 

截圖供對話與未完全充滿lisview: http://imageshack.us/photo/my-images/802/dialog.png/

我怎樣才能讓onFling()事件對整個對話或在「整體」的ListView觸發即使它沒有完全充滿物品?

謝謝!

回答

5

您應該創建延伸的對話框類的類,然後你可以創建一個姿勢檢測並連接到類的onTouchEvent。這裏是一個代碼示例:

public class GestureDialog extends Dialog { 
    public GestureDialog (Context context, int theme) { 
     super(context, theme); 

     gestDetec = new MyGestureDetector(); //inital setup 
     gestureDetector = new GestureDetector(gestDetec); 
     gestureListener = new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       if (gestureDetector.onTouchEvent(event)) { 
        return true; 
       } 
       return false; 
      } 
     }; 
    } 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (gestureDetector.onTouchEvent(event)) 
      return true; 
     else 
      return false; 
    } 
class MyGestureDetector extends SimpleOnGestureListener { 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

     System.out.println("Help im being touched!"); 
     return false; 
    } 
} 

} 
0

您可以取一個activity並將其設置爲theme明顯爲dialog

然後implements onTouchListener到該活動 並在該活動中寫入一個onTouch()事件。

現在,你有觸摸事件的整個對話:)