无法在Android应用上执行GestureDetector的任何实现 [英] No implementation of GestureDetector will work on android app

查看:95
本文介绍了无法在Android应用上执行GestureDetector的任何实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将GestureDetector添加到我的应用程序中的活动。我尝试了来自youtube和stackoverflow的几种不同方法,但都没有用。滑动或使用任何手势时都不会显示任何错误,并且什么也不会发生。我一直在进行调试日志,并且现在都没有想法在logcat中注册。我主要感兴趣的是onFling()方法。

I am trying to add a GestureDetector to an activity in my app. I have tried several different methods from youtube and stackoverflow none are working. No errors are showing and nothing happens when I swipe or use any gesture. I have put debugging logs all along the way and none are registering in the logcat Im all out of ideas now. It is the onFling() method I am mainly interested in.

我提出了两种我尝试过的方法。我已经删除了一些代码,并对其进行了更改,以使读者更加友好和相关。

I am putting up two methods i have tried. I have removed some code and changed it to make more reader friendly and relevant.

如果有人可以帮助Id感到高兴。

If anyone can help Id be delighted.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">

    <!-- LinearLayout has two children, Toolbar & DrawerLayout.
            DrawerLayout has two children RelativeLayout (main content container) 
            LinearLayout is the root element so the Navigationdrawer does not open over the Toolbar.
            The DrawerLayout was the root element before this.-->


    <!-- adding the toolbar layout -->
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>


<android.support.v4.widget.DrawerLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainDrawerLayout">


         <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
        android:id="@+id/backgroundImg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/nipple"
        android:contentDescription="@string/content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />


             <TextView android:id="@+id/main_tv"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:padding="10dp"
                 android:gravity="center"
                 android:text="Hello"/>

    </RelativeLayout>

    <fragment android:id="@+id/fragment_navigation_drawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        android:name="com.ansgar.amazingfacts.activities.FragmentNavigationDrawer"
        tools:layout="@layout/fragment_navigation_drawer"/>


</android.support.v4.widget.DrawerLayout>
    </LinearLayout>

方法1定义自定义侦听器类

Method 1 Defining custom listener class

public class MainActivity extends AppCompatActivity implements FragmentNavigationDrawer.FragmentNavigationDrawerListener {
    private ImageView image;
    private TextView mTextView;
    private Toolbar mToolbar;
    private FragmentNavigationDrawer drawerFragment;
    GestureDetectorCompat gesture;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.backgroundImg);
        mTextView = (TextView) findViewById(R.id.main_tv);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        //Fragment for navigation drawer
        drawerFragment = (FragmentNavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        //This method is created because we need to pass a few things from MainActivity to the FragmentNavigationDrawer
        drawerFragment.sepUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(mainDrawerLayout), mToolbar);
        drawerFragment.setDrawerListener(this);

        this.gesture = new GestureDetectorCompat(this, new CustomGestureClass());

    }//onCreate()



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.gesture.onTouchEvent(event);
        Log.d("GESTURE", "OnTouchEvent");
        Toast.makeText(getApplicationContext(), "Touch event", Toast.LENGTH_SHORT).show();
        return super.onTouchEvent(event);
    }


        class CustomGestureClass extends GestureDetector.SimpleOnGestureListener {
            //SimpleOnGestureListener is listener for what we want to do and how

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                //return super.onFling(e1, e2, velocityX, velocityY);
                Log.d("GESTURE", "OnFling");
                float sensitivity = 50;
                //swipe left check
                if (e1.getX() - e2.getX() > sensitivity) {
                    Log.d("GESTURE", "swipe left");
                    return true;
                }

                //swipe right check
                if (e2.getX() - e1.getX() > sensitivity) {
                    Log.d("GESTURE", "Swipe Right");
                    Toast.makeText(getApplicationContext(), "Swipe right gesture", Toast.LENGTH_SHORT).show();
                    return true;
                }

                //swipe check down
                if (e1.getY() - e2.getY() > sensitivity) {
                    Log.d("GESTURE", "Swipe Down");
                    Toast.makeText(getApplicationContext(), "Swipe down gesture", Toast.LENGTH_SHORT).show();
                    return true;
                }

                //swipe up check
                if (e2.getY() - e1.getY() > sensitivity) {
                    Log.d("GESTURE", "Swipe Up");
                    Toast.makeText(getApplicationContext(), "Swipe up gesture", Toast.LENGTH_SHORT).show();
                    return true;
                }

                return true;
                //return false;
            }

            @Override
            public boolean onDown(MotionEvent e) {
                Log.d("GESTURE", "OnDown");
                //return super.onDown(e);
                return true;
            }
        }//CustomGestureClass



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;

    }

    //setting the title of actionBar
    public void setTitle(String title){
        getSupportActionBar().setTitle(title);
    }

    public void onCreateDrawer() {}

    @Override
    public void onDrawerItemSelected(View view, int position){
        displayView(position);
        //Log.d("DRAWER ITEM SELECTED", "the index number of item Drawer RecyclerView "+ position);
        switch(position){

            case 0:
                break;
        }
    }

    private void displayView(int position) {}

}//MainActivity.class

方法2

public class MainActivity extends AppCompatActivity implements FragmentNavigationDrawer.FragmentNavigationDrawerListener,
    GestureDetector.OnGestureListener {

    private ImageView image;
    private TextView mFactTextView;
    private Toolbar mToolbar;
    private FragmentNavigationDrawer drawerFragment;
    GestureDetectorCompat gesture;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.backgroundImg);
        mTextView = (TextView) findViewById(R.id.main_tv);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        //Fragment for navigation drawer
        drawerFragment = (FragmentNavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        //This method is created because we need to pass a few things from MainActivity to the FragmentNavigationDrawer
        drawerFragment.sepUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(mainDrawerLayout), mToolbar);
        drawerFragment.setDrawerListener(this);


        this.gesture = new GestureDetectorCompat(this, this);

    }//onCreate()


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.gesture.onTouchEvent(event);
        Log.d("GESTURE", "OnTouchEvent");
        Toast.makeText(getApplicationContext(), "Touch event", Toast.LENGTH_SHORT).show();
        return super.onTouchEvent(event);
    }


    @Override
    public boolean onDown(MotionEvent e) {
        Log.d("GESTURE", "OnDown");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        Log.d("GESTURE", "OnShowPress");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Log.d("GESTURE", "OnSingleTapUp");
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.d("GESTURE", "OnScroll");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        Log.d("GESTURE", "OnLongPress");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.d("GESTURE", "OnFling");
        return true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void onCreateDrawer() {}


    @Override
    public void onDrawerItemSelected(View view, int position){
        displayView(position);
        //Log.d("DRAWER ITEM SELECTED", "the index number of item Drawer RecyclerView "+ position);
        switch(position){
            case 0:
                break;
        }
    }

    private void displayView(int position) {}


}//MainActivity.class


推荐答案

关于onTouchEvent(event)方法的文档在触摸屏事件发生时调用未被其下的任何视图处理。这对于处理在窗口范围之外发生的触摸事件最有用,那里没有视图可以接收它。。因此,您应该检查:

Concerning to the document of onTouchEvent(event) method: "Called when a touch screen event was not handled by any of the views under it. This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it.". So you should check:


  1. 在活动中是否有处理该事件的子视图(这意味着onTouchEvent此子视图的(事件)方法返回
    true )。如果不是,请转到数字2

  1. There is a child view that was handled the event in the activity or not (that means the onTouchEvent(event) method of this child view returns "true"). If no, go to number 2

应返回活动的onTouchEvent(event)方法
true 。(根据文档: 如果您有
消费了事件,则返回true,否则,则返回false。默认实现
始终返回false
->使用 return super.onTouchEvent(event); 始终返回 false

The onTouchEvent(event) method of the activity should be returned "true".(according the document: "Return true if you have consumed the event, false if you haven't. The default implementation always returns false" -> using return super.onTouchEvent(event); always returns "false")

希望它可以为您提供帮助

Hope it can help you

这篇关于无法在Android应用上执行GestureDetector的任何实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆