問題描述
我有一個對話框可以從中選擇文件.此對話框包含一個用于顯示目錄和文件的列表視圖,并具有一個用于導航目的的 onItemClickListener.
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.
我如何對在目錄結構中上升的投擲事件做出反應?我嘗試在根 RelativeLayout 上設置 OnTouchListener 以將觸摸事件發送到我的 GestureListener:
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;
}
});
事件在 onTouch() 中注冊,但從未在 MyGestureDetector 中調用 onFling() 方法.但是,如果我將 OnTouchListener 附加到列表視圖,它會按預期工作.這種方法的問題是列表視圖并不總是大量填充數據,當它為空或單擊下面的項目時,不會觸發任何 onTouch() 事件.
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.
對話框布局:
<?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/
Screenshot for dialog with not completely filled lisview: http://imageshack.us/photo/my-images/802/dialog.png/
如何使 onFling() 事件在整個對話框或整個"列表視圖上觸發,即使它沒有完全填充項目?
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?
謝謝!
推薦答案
您應該創建一個擴展對話框類的類,然后您可以創建一個手勢檢測器并附加到該類的 ontouchevent.這是一個代碼示例:
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;
}
}
}
這篇關于整個對話框的Android onTouchListener的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!