2016-12-21 5 views
0

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> 
+0

Avez-vous regardé cela? http://techin-android.blogspot.co.il/2011/11/swipe-event-in-android-scrollview.html – baselsader

Répondre

0

Vous pouvez essayer une draglistener, sans mettre en oeuvre l'interface d'ombre:

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

essayé. mais même résultat. 'DragEvent.ACTION_DRAG_ENDED:' se produit uniquement en un seul clic. pas en faisant glisser et relâcher – Drd

+0

Pouvez-vous s'il vous plaît poster votre mise en page xml, de sorte que je puisse comprendre quelle vue obtient le touchlistener? – baselsader

+0

a ajouté le code complet – Drd