J'ai une vue à l'intérieur d'un NestedScrollView
. En balayant cette vue, je dois trouver la direction de balayage. Que ce soit glissé vers le haut ou vers le bas. Comment puis-je trouver ceci.Comment trouver les positions touchées et libérées d'une vue
Ce que j'ai essayé est, j'ajouté OnTouchListener
à la vue, et je pensais que de trouver la différence entre le motionEvent.getY()
dans MotionEvent.ACTION_DOWN
et MotionEvent.ACTION_UP
.
Mais le problème est MotionEvent.ACTION_UP
est appelée seulement sur un seul robinet. Lorsque je presse et déplacez puis relâchez MotionEvent.ACTION_UP
n'est jamais arrivé.
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);
}
Ici case MotionEvent.ACTION_UP
est jamais quand je passe Faites glisser votre doigt et relâchez.
Quelle est la meilleure façon de résoudre mon problème?
Voici mon code complet
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 est
<?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>
Avez-vous regardé cela? http://techin-android.blogspot.co.il/2011/11/swipe-event-in-android-scrollview.html – baselsader