Android整个对话框的onTouchListener

3

我有一个选择文件的对话框。该对话框包含一个列表视图,用于显示目录和文件,并具有onItemClickListener以进行导航。

如何响应fling事件以向上导航到目录结构? 我尝试在根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()中注册,但是MyGestureDetector中的onFling()方法从未被调用。然而,如果我将OnTouchListener附加到listview,则它按预期工作。这种方法的问题是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>

未完全填写的列表对话框截图: http://imageshack.us/photo/my-images/802/dialog.png/

如何使 onFling() 事件在整个对话框或“整个”列表视图上触发,即使它没有完全填充项目?

谢谢!

2个回答

6

您需要创建一个继承对话框类的类,然后可以创建一个手势检测器并将其附加到该类的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;
    }
}

}

1

您可以在清单文件中将一个activitytheme设置为dialog

然后,在该活动中implements onTouchListener并编写一个onTouch()事件。

现在,您已经获得了整个对话框的触摸事件 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接