Android onTouchListener 用于整个对话框

发布于 2024-12-11 07:55:33 字数 3306 浏览 0 评论 0原文

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

我如何对 fling 事件做出反应以在目录结构中上升?我尝试在根相对布局上设置 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 附加到列表视图,它就会按预期工作。这种方法的问题是列表视图并不总是充满数据,当列表视图为空或单击下面的项目时,不会触发 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() 事件在整个对话框或“整个”列表视图上触发,即使它没有完全填充项目?

谢谢你!

I have a dialog to choose files from. This dialog contains a listview to show directories and files and has an onItemClickListener for navigation purposes.

How can I react to fling events to go up in the directory structure? I tried setting an OnTouchListener on the root RelativeLayout to dispatches touch events to my 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;
            }
        });

The events get registered in onTouch(), but the onFling() method is never called in the MyGestureDetector. If, however, I attach the OnTouchListener to the listview, it works as expected. The problem with this approach is that the listview is not always filled extensively with data and when it is empty or when clicking below items, no onTouch() events are triggered.

Dialog layout:

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

Screenshot for dialog with not completely filled lisview:
http://imageshack.us/photo/my-images/802/dialog.png/

How can I make the onFling() events trigger on the whole dialog or on the "whole" listview even when it's not completely filled with items?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看海 2024-12-18 07:55:33

您应该创建一个扩展对话框类的类,然后您可以创建一个手势检测器并附加到该类的 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;
    }
}

}

You should create a class which extends the dialog class and then you can create a gesture detector and attach to the ontouchevent of the class.
Here is a code example:

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;
    }
}

}
东京女 2024-12-18 07:55:33

您可以选择一个 activity 并将其清单中的 theme 设置为 dialog

然后为该活动实现 onTouchListener
并在该 Activity 中编写一个 onTouch() 事件。

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

You can Take one activity and set it's theme in manifest to dialog.

Then implements onTouchListener to that activty
and write one onTouch() event in that Activity.

Now you got Touch event for entire dialog :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文