在片段导航抽屉上捕获手势

时间:2014-06-29 05:55:05

标签: android

我已为我的应用实施了导航抽屉。 我试图捕捉向上和向下滑动手势。但问题是导航抽屉需要片段而不是活动。

我尝试添加一个触摸侦听器来查看膨胀布局文件。但似乎没有任何效果。

__ EDIT ____

在尝试了很多方法后,我似乎得到了一个解决方案。我能够将OnTouch事件传递给Activity,然后进行了一些更改。

当有人触摸它时,我没有尝试关闭导航抽屉。

解决方案:

1)创建一个自定义DrawerLayout并覆盖它的OnTouchEvent返回false。

package com.example.navigationdrawertest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class CustomDrawerLayout extends android.support.v4.widget.DrawerLayout {

public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public CustomDrawerLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomDrawerLayout(Context context) {
    super(context);
}

@Override
public boolean onTouchEvent(MotionEvent arg0) {
    return false;
}



}

2)在XML布局文件中实现CustomDrawerLayout。

<com.example.navigationdrawertest.CustomDrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- The main content view -->

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#299"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

</com.example.navigationdrawertest.CustomDrawerLayout>

3)获取活动中自定义抽屉布局的参考

com.example.navigationdrawertest.CustomDrawerLayout custom= (com.example.navigationdrawertest.CustomDrawerLayout) findViewById(R.id.drawer_layout);

4)创建一个类来识别手势。

class MyGestureDetector extends SimpleOnGestureListener{
}

5)覆盖onDown和onFling事件。必须在onDown中返回true。

在onFling中,我一直在寻找滑动。左右滑动应打开和关闭抽屉。

class MyGestureDetector extends SimpleOnGestureListener{

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        float diffX = Math.abs(e2.getX() - e1.getX());
        float diffY = Math.abs(e2.getY() - e1.getY());

        if (diffX > diffY) {
            if (e2.getX() > e1.getX()) {
                custom.openDrawer(Gravity.LEFT);
            } else {
                custom.closeDrawer(Gravity.LEFT);
            }
        } else if (diffX < diffY) {
            if (e2.getY() > e1.getY()) {
                Toast.makeText(getApplicationContext(), "Down",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "UP",
                        Toast.LENGTH_SHORT).show();
            }
        }

        return true;
    }

}

6)最后覆盖Activity的onTouchEvent并创建GestureDetector。

GestureDetectorCompact gesture ;
gesture= new GestureDetectorCompat(this, new MyGestureDetector());


@Override
public boolean onTouchEvent(MotionEvent event) {
    gesture.onTouchEvent(event);
    return super.onTouchEvent(event);
}

7)最后完整的活动看起来像这样。

package com.example.navigationdrawertest;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

ListView ll;
FragmentManager fm;
GestureDetectorCompat gesture;
com.example.navigationdrawertest.CustomDrawerLayout custom;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    //Getting reference to CustomDrawerLayout.
    custom = (com.example.navigationdrawertest.CustomDrawerLayout) findViewById(R.id.drawer_layout);

    //Reference to ListView
    ll = (ListView) findViewById(R.id.left_drawer);

    //Inflating out Fragment
    fm = getFragmentManager();
    fm.beginTransaction()
            .replace(R.id.content_frame, new YetAnotherActivity()).commit();

    //New GestureDetector Class
    gesture = new GestureDetectorCompat(this, new MyGestureDetector());
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    gesture.onTouchEvent(event);
    return super.onTouchEvent(event);
}


class MyGestureDetector extends SimpleOnGestureListener{

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        float diffX = Math.abs(e2.getX() - e1.getX());
        float diffY = Math.abs(e2.getY() - e1.getY());

        if (diffX > diffY) {
            if (e2.getX() > e1.getX()) {
                custom.openDrawer(Gravity.LEFT);
            } else {
                custom.closeDrawer(Gravity.LEFT);
            }
        } else if (diffX < diffY) {
            if (e2.getY() > e1.getY()) {
                Toast.makeText(getApplicationContext(), "Down",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "UP",
                        Toast.LENGTH_SHORT).show();
            }
        }

        return true;
    }

}
}

那就是我如何完成NavigationDrawer +手势。

0 个答案:

没有答案