我正在尝试在我的应用中实现滑动手势.我已经制作了几乎所有的代码,但它不起作用.
这是我在Activity中的代码:
// Swipe detector
gestureDetector = new GestureDetector(new SwipeGesture(this));
gestureListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
Log.e("", "It works");
return gestureDetector.onTouchEvent(event);
}
};
LinearLayout root = (LinearLayout) findViewById(R.id.rules_root);
root.setOnTouchListener(gestureListener);
Run Code Online (Sandbox Code Playgroud)
当我触摸屏幕时,会显示logcat it works.
在这里他是我的班级代码SwipeGesture:
public class SwipeGesture extends SimpleOnGestureListener
{
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private Activity activity;
public SwipeGesture(Activity activity)
{
super();
this.activity = activity; …Run Code Online (Sandbox Code Playgroud) 在我的viewFlipper中,一些TextViews是动态加载的.大小可能不同,这意味着在viewFlipper下可能会留下一些空间(请参见截图中的绿色部分)
我希望不仅在灰色部分(即viewflipper)上滑动时调用onFling方法,而且还要在绿色部分上滑动时调用onFling方法
我的布局看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/root">
<ViewFlipper android:id="@+id/viewFlipper"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ViewFlipper>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
在我的onCreate中,我这样做:
this.viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
this.gestureDetector = new GestureDetector(new MyGestureDetector());
RelativeLayout root = (RelativeLayout) this.findViewById(R.id.root);
root.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
Log.d("root", "true");
return false;
} else {
Log.d("root", "false");
return false;
}
}
});
Run Code Online (Sandbox Code Playgroud)
到目前为止,即使我得到了真的,我也试图返回false,这样即使在viewflipper之外进行了滑动,事件也不会被消耗掉并传递给viewFlipper.
请注意,viewFlipper不需要任何显式的onTouchListener.它有或没有一个(我真的不明白为什么..)
有谁知道该怎么办?
我有一个ViewPager持有一个RecyclerView我添加OnTouchListener到里面的形象图RecyclerView的项目,试图检测使用触摸GestureDetector.SimpleOnGestureListener,但我不能得到的onFling事件。我怀疑它的发生是因为RecyclerView或ViewPager捕捉运动事件。我如何解决它?
我的代码(虽然它很简单):
public class MyClickListener extends GestureDetector.SimpleOnGestureListener{
public MyClickListener(){
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("aaa", "show press");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("aaa", "tapup");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
Log.d("aaa", "scroll"); …Run Code Online (Sandbox Code Playgroud) 任何人都可以举例说明如何在android中的webview中实现手势检测器
谢谢
我的应用程序中有一个自定义图库,经过一些测试后我决定不想让图库用手指滑动导航.我已经设置了一个左右按钮来控制它.现在我想弄清楚如何禁用该onFling方法.我已经尝试了this.setEnabled(false);哪些不起作用,并尝试了this.setClickable(false);哪些不起作用...我的重写onFling()方法除了返回(true)之外还有其他一切; 评论....不知道还有什么可以尝试!有任何想法吗??
谢谢 :)
我正在实现一个onFling探测器,就像下面的问题一样: 在网格布局上进行手势检测
但是,我无法让它发挥作用.
如果某种方式我设置我的应用程序导致此问题(可能是asynctask?),这是我的应用程序的结构:
(试图使它成为一个格式良好的列表,但由于某种原因它搞砸了它下面的代码?) -
MapActivity创建一个MapView.
--MapView创建一个ASyncTask来从URL获取XML.
ASyncTask的--onPostExecute()解析XML并使用获取的数据添加ItemizedOverlay - ItemizedOverlay的
--onTap()函数使用LoaderImageView从Web获取图像(http://www.anddev.org/novice-tutorials -f8/imageview -with-loading-spinner-t49439.html) -
onTap()函数然后调用下面列出的PopupPanel类的show()函数
从下面的代码中可以看出,注释掉的行有效.但是,与MyGestureDetector类一起使用时,onTouch和onFling事件永远不会被命中.
class PopupPanel {
View popup;
boolean isVisible = false;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
public View.OnTouchListener gestureListener;
PopupPanel(Context context, int layout) {
ViewGroup parent = (ViewGroup) map.getParent();
popup = ((MapActivity) context).getLayoutInflater().inflate(layout, parent, false);
SelectFilterActivity selectFilterActivity = new SelectFilterActivity();
popup.setOnClickListener(selectFilterActivity);
// This works! …Run Code Online (Sandbox Code Playgroud) 我有一个对话框来选择文件.此对话框包含用于显示目录和文件的列表视图,并具有用于导航目的的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;
}
});
Run Code Online (Sandbox Code Playgroud)
事件在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" …Run Code Online (Sandbox Code Playgroud)