如何查找视图的触摸和释放位置

时间:2016-12-21 14:47:19

标签: android ontouchlistener motionevent android-gesture

我在NestedScrollView内有一个视图。在滑动此视图时,我必须找到滑动方向。无论是向上还是向下滑动。我怎么能找到这个。

我尝试的是,我在视图中添加了OnTouchListener,并且我想找到motionEvent.getY()MotionEvent.ACTION_DOWN中的MotionEvent.ACTION_UP之间的区别。

但问题是MotionEvent.ACTION_UP仅在单击时被调用。当我按下并移动然后释放时MotionEvent.ACTION_UP永远不会发生。

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

当我滑动并释放时,case MotionEvent.ACTION_UP永远不会发生。

解决问题的最佳方法是什么?

这是我的完整代码

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

<?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 个答案:

答案 0 :(得分:0)

您可以尝试使用draglistener,而无需实现阴影界面:

 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);