如何同时为导航抽屉和视图启用触摸事件?

时间:2016-01-07 12:19:08

标签: android ontouchlistener ontouchevent android-tablelayout android-navigation-drawer

我创建了带导航抽屉的活动(默认的Android Studio活动)。此活动扩展了AppCompatActivity并实现了NavigationView.OnNavigationItemSelectedListener

public class CashDeskActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

我在活动的布局中有TableLayout。此表显示活动中的数据表。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="uz.cpeople.ox.activities.CashDeskActivity"
    tools:showIn="@layout/app_bar_cash_desk">

    <uz.cpeople.ox.ui.scroll.VScroll
        android:id="@+id/vScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <uz.cpeople.ox.ui.scroll.HScroll
            android:id="@+id/hScroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TableLayout
                android:id="@+id/tlVariations"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF" >
            </TableLayout>

        </uz.cpeople.ox.ui.scroll.HScroll>

    </uz.cpeople.ox.ui.scroll.VScroll>


</LinearLayout>

为什么我需要VScroll和HScroll?设备屏幕不足以显示表中的所有数据。为了查看更多数据,用户可以同时水平和垂直滚动。我为此目的使用了solution

enter image description here

当我在没有导航抽屉的活动中测试此解决方案时,它完全滚动。如果Activity有导航抽屉,则触摸事件不起作用:

@Override
public boolean onTouchEvent(MotionEvent event) {

    float curX, curY;
    Galgo.log("TOUCH");

    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            Galgo.log("UP:"+mx+","+my);
            mx = event.getX();
            my = event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            Galgo.log("UP:"+mx+","+my);
            curX = event.getX();
            curY = event.getY();
            vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            mx = curX;
            my = curY;
            break;
        case MotionEvent.ACTION_UP:
            Galgo.log("UP:"+mx+","+my);
            curX = event.getX();
            curY = event.getY();
            vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            break;
    }

    return false;
}

然后我试图解决这个问题,android fount这个(但在这种情况下导航抽屉不起作用)

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    float curX, curY;

    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            mx = event.getX();
            my = event.getY();
            break;
        case MotionEvent.ACTION_MOVE:

            curX = event.getX();
            curY = event.getY();
            vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            mx = curX;
            my = curY;
            break;
        case MotionEvent.ACTION_UP:

            curX = event.getX();
            curY = event.getY();
            vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
            break;
    }

    return false;
}

我对方法dispatchTouchEvent,onTouchEvent感到困惑。我想了解为什么它没有按预期工作。如何为导航抽屉和其他视图启用触摸事件(ScrollView,TableLayou)?帮助我理解。

0 个答案:

没有答案