2011-10-21 13 views
1

ファイルを選択するダイアログがあります。このダイアログには、ディレクトリとファイルを表示するためのリストビューが含まれており、ナビゲーション目的でonItemClickListenerがあります。ダイアログ全体のAndroid onTouchListener

フリングイベントに反応してディレクトリ構造に入ることはできますか?私は私のGestureListenerにタッチイベントディスパッチにルートRelativeLayoutにOnTouchListenerを設定してみました:

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()イベントがトリガーされないことです。

ダイアログレイアウト:完全に満たされていないlisviewとの対話のための

<?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> 

スクリーンショット:どのように私はonFling()イベントはダイアログ全体の上または「全体」リストビューでトリガ http://imageshack.us/photo/my-images/802/dialog.png/

を作ることができますそれがアイテムで完全に満たされていないときでさえ?

ありがとうございました!

答えて

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を1つ取り、マニフェストのthemedialogに設定します。

次に、そのアクティビティ にimplements onTouchListenerを書き込み、そのアクティビティにonTouch()イベントを書き込みます。

は今、あなたはダイアログ全体のためのタッチイベントを得た:)