How to find the touched and released positions of a view

222 views Asked by At

I have a view inside a NestedScrollView. On swiping this view I have to find the swipe direction. Whether swiped up or down. How can I find this.

What I tried is , I added OnTouchListener to the view , and I thought of finding the difference between the motionEvent.getY() in MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP.

But the problem is MotionEvent.ACTION_UP is called only on single tap. When I press and move and then release MotionEvent.ACTION_UP is never happened.

 public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            log("Down");
            break;
        case MotionEvent.ACTION_UP:
            log("up");
            break;

    }
    return true;
}

private void log(String s) {
    Log.d(DEBUG_TAG, "" + s);
}

Here case MotionEvent.ACTION_UP is never happening when I swipe and release.

What is the best way for solving my issue?

Here's my full code

public class MainActivity extends AppCompatActivity {
private static final String DEBUG_TAG = MainActivity.class.getSimpleName();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final NestedScrollView nestedScrollView = (NestedScrollView) findViewById(R.id.scroll);
    nestedScrollView.setSmoothScrollingEnabled(true);
    View layout1 = findViewById(R.id.layout1);
    layout1.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View view, DragEvent event) {
            int action = event.getAction();
            int start_x;
            int start_y;
            int end_x;
            int end_y;

            switch (action) {

                case DragEvent.ACTION_DRAG_ENTERED:
                    return false;

                case DragEvent.ACTION_DRAG_EXITED:
                    return false;

                case DragEvent.ACTION_DRAG_STARTED:
                    start_x = (int) event.getX();
                    start_y = (int) event.getY();
                    log("touched");
                    return true;

                case DragEvent.ACTION_DRAG_LOCATION:
                    return false;

                case DragEvent.ACTION_DROP:
                    return false;

                case DragEvent.ACTION_DRAG_ENDED:
                    end_x = (int) event.getX();
                    end_y = (int) event.getY();
                    log("released");
                    break;

                default:
                    return true;

            }
            return true;
        }
    });
}

private void log(String s) {
    Log.d(DEBUG_TAG, "" + s);
}

 }

XML is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/activity_root_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.test.test.MainActivity">
<android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/root_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="1000dp"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="PAGE 1"/>


        </LinearLayout>
        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="match_parent"
            android:layout_height="1000dp"
            android:layout_below="@id/layout1"
            android:background="@color/colorPrimaryDark"
            android:gravity="center"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="PAGE 2"/>
        </LinearLayout>

    </RelativeLayout>
  </android.support.v4.widget.NestedScrollView>
 </LinearLayout>
1

There are 1 answers

2
baselsader On

You can try a draglistener, without implementing the shadow interface:

 View.OnDragListener dragListener = new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {

            int action = event.getAction();
            int start_x;
            int start_y;
            int end_x;
            int end_y;

            switch (action) {

                case DragEvent.ACTION_DRAG_ENTERED:
                    return false;

                case DragEvent.ACTION_DRAG_EXITED:
                    return false;

                case DragEvent.ACTION_DRAG_STARTED:
                    start_x = event.getX();
                    start_y = event.getY();
                    return true;

                case DragEvent.ACTION_DRAG_LOCATION:
                    return false;

                case DragEvent.ACTION_DROP:
                    return false;

                case DragEvent.ACTION_DRAG_ENDED:
                    end_x = event.getX();
                    end_y = event.getY();
                    break;

                default:
                    return true;

            }
            return true;
        }
    };

    layout.setOnDragListener(dragListener);