使用ScrollView内容和左右滑动手势检测的DrawerLayout

时间:2014-05-21 17:40:43

标签: android scrollview swipe drawerlayout gesturedetector

我有一个带有ScrollView的DrawerLayout。我希望能够在ScrollView上的任何位置水平滑动,并让它打开/关闭DrawerLayout(而不是仅仅在边缘)。我想我必须使用手势检测才能做到这一点但是当我向MainActivity添加手势检测时,它还会在您沿对角线滑动时滚动底层的ScrollView,这是我不希望发生的事情。这是Github上的一个示例项目:

https://github.com/timothyjc/GestureProblem

这是我的MainActivity:

package com.example.examplegestureproblem;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

public class MainActivity extends ActionBarActivity {

private static final String TAG = MainActivity.class.getSimpleName() + "!!!!!!!!!!!!!!!!!!!1";

private GestureDetector mDetector;
private DrawerLayout drawerLayout;
private View leftDrawer;
private View rightDrawer;

private int swipeThreshold;
private int swipVelocityThreshold;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    leftDrawer = findViewById(R.id.left_drawer);
    rightDrawer = findViewById(R.id.right_drawer);
    mDetector = new GestureDetector(this, new MyGestureListener());

    ViewConfiguration configuration = ViewConfiguration.get(this);
    swipVelocityThreshold = configuration.getScaledMinimumFlingVelocity();
    swipeThreshold = configuration.getScaledTouchSlop();
}

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

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    super.dispatchTouchEvent(ev);
    return mDetector.onTouchEvent(ev);
}

class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent event) {
        Log.d(TAG, "onDown: " + event.toString());
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            float deltaX = e2.getRawX() - e1.getRawX();
            float deltaY = e2.getRawY() - e1.getRawY();

            if (Math.abs(deltaX) > Math.abs(deltaY)) {
                if (Math.abs(deltaX) > swipeThreshold && Math.abs(velocityX) > swipVelocityThreshold) {
                    if (deltaX > 0) {
                        Log.i(TAG, "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
                        onSwipeRight();
                        return true;
                    } else {
                        Log.i(TAG, "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
                        onSwipeLeft();
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "opps", e);
        }
        return false;
    }

}

public void onSwipeRight() {
    Log.i(TAG, "onSwipeRight");
    if (drawerLayout.isDrawerVisible(rightDrawer)) {
        drawerLayout.closeDrawer(rightDrawer);
    } else if (!drawerLayout.isDrawerVisible(leftDrawer)) {
        drawerLayout.openDrawer(leftDrawer);
    }
}

public void onSwipeLeft() {
    Log.i(TAG, "onSwipeLeft");
    if (drawerLayout.isDrawerVisible(leftDrawer)) {
        drawerLayout.closeDrawer(leftDrawer);
    } else if (!drawerLayout.isDrawerVisible(rightDrawer)) {
        drawerLayout.openDrawer(rightDrawer);
    }
}

}

这是我的DrawerLayout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
    android:id="@+id/content"
    android:name="com.example.examplegestureproblem.ContentFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<View
    android:id="@+id/left_drawer"
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#44aaaa"
    android:clickable="true" />

<View
    android:id="@+id/right_drawer"
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="#44aa44"
    android:clickable="true" />

这是内容片段:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroller"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="30dp" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click this" />

    <SeekBar
        android:id="@+id/seek"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10" />

    <View
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#aa4444"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#4444aa"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#44aaaa" />

    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#aaaa44" />

    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#4444aa" />

    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#aa4444" />

    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#444444" />

    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#aaaaaa" />

    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#6622bb" />

    <View
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#aa44aa" />
</LinearLayout>

0 个答案:

没有答案