【发布时间】:2017-08-03 01:01:06
【问题描述】:
我在NestedScrollView 中有一个视图。在滑动这个视图时,我必须找到滑动方向。无论是向上还是向下滑动。我怎样才能找到这个。
我尝试的是,我在视图中添加了OnTouchListener,我想找到MotionEvent.ACTION_DOWN和MotionEvent.ACTION_UP中的motionEvent.getY()之间的区别。
但问题是 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>
【问题讨论】:
标签: android ontouchlistener motionevent android-gesture