1

I have created Activity with Navigation Drawer (default Android Studio activity) . This activity extends AppCompatActivity and implements NavigationView.OnNavigationItemSelectedListener

public class CashDeskActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

I have TableLayout inside activity's layout. This table shows data table in activity.

<?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>

Why I need VScroll and HScroll? Device screen is not enough to show all data in table. In order to see more data, user can scroll horizontally and vertically at the same time. I used this solution for this purpose.

enter image description here

When I tested this solution in activity without navigation drawer, it is scrolling perfectly. If Activity has Navigation Drawer, touch event is not working:

@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;
}

Then I tried to solve this problem, android fount this (but in this case navigation drawer is not working)

@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;
}

I am confused on methods dispatchTouchEvent, onTouchEvent. I want to understand why it is not working as expected. How to enable touch event for navigation drawer and other view (ScrollView, TableLayou)? Help me to understand.

Community
  • 1
  • 1
Joe Richard
  • 1,520
  • 7
  • 20
  • 31

0 Answers0