5

Sto usando un FrameLayout per eseguire il gesto di scorrimento di ciascuna delle mie carte, ma le carte non sempre scartano. E a volte si bloccano solo a sinistra o a destra finché l'utente non fa scorrere la carta una seconda volta.Le mie cardview non verranno sempre ignorate quando si scorre verso sinistra o destra?

Come posso risolvere questo?

MyFrameLayout:

public class MyFrameLayout extends FrameLayout { 

    private static int mWidth = 200; 
    MyFrameLayout touchFrameLayout; 

    // Constructors 
    // i call "initialize(context)" in all of them 

    private void initialize(Context context) { 
     setOnTouchListener(mTouchListener); 
     touchFrameLayout = this; 

    } 

    private float mDisplacementX; 
    private float mDisplacementY; 
    private float mInitialTx; 
    private boolean mTracking; 
    private OnTouchListener mTouchListener = new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      mWidth = (int) touchFrameLayout.getLayoutParams().width; 
      switch (event.getActionMasked()) { 
       case MotionEvent.ACTION_DOWN: 

        mDisplacementX = event.getRawX(); 
        mDisplacementY = event.getRawY(); 

        mInitialTx = getTranslationX(); 

        return true; 

       case MotionEvent.ACTION_MOVE: 
        // get the delta distance in X and Y direction 
        float deltaX = event.getRawX() - mDisplacementX; 
        float deltaY = event.getRawY() - mDisplacementY; 
        // updatePressedState(false); 

        // set the touch and cancel event 
        if ((Math.abs(deltaX) > ViewConfiguration.get(getContext()) 
          .getScaledTouchSlop() * 2 && Math.abs(deltaY) < Math 
          .abs(deltaX)/2) 
          || mTracking) { 

         mTracking = true; 

         if (getTranslationX() <= mWidth/2 
           && getTranslationX() >= -(mWidth/2)) { 

          setTranslationX(mInitialTx + deltaX); 
          return true; 
         } 
        } 

        break; 

       case MotionEvent.ACTION_UP: 

        if (mTracking) { 
         mTracking = false; 
         float currentTranslateX = getTranslationX(); 

         if (currentTranslateX > (mWidth/10)) { 
          rightSwipe(); 
         } else if (currentTranslateX < -(mWidth*10)) { 
          leftSwipe(); 
         } 

         // comment this line if you don't want your frame layout to 
         // take its original position after releasing the touch 
         setTranslationX(0); 
         return true; 
        } else { 
         // handle click event 
         setTranslationX(0); 
        } 
        break; 
      } 
      return false; 
     } 
    }; 

    private void rightSwipe() { 
     Toast.makeText(this.getContext(), "Swipe to the right", 
       Toast.LENGTH_SHORT).show(); 
    DeleteCard(); 
    } 

    private void leftSwipe() { 
     Toast.makeText(this.getContext(), "Swipe to the left", 
       Toast.LENGTH_SHORT).show(); 
     DeleteCard(); 
    } 
} 

Xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.example.testing.android.layout.MyFrameLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

     <android.support.v7.widget.CardView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      xmlns:card_view="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/taskViewContainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      app:cardElevation="8dp" 
      app:cardPreventCornerOverlap="false" 
      card_view:cardCornerRadius="8dp"> 
     </android.support.v7.widget.CardView> 

    </com.example.testing.android.layout.MyFrameLayout> 

</RelativeLayout> 
+0

Hai un elenco o solo un singolo CardView come mostra il tuo xml? –

risposta

0

mi sono adattato di romannurik Android-SwipeToDismiss di fare esattamente questo.

Il codice inizia github con un'applicazione di esempio woking, e consiste in:

Una sottoclasse di RecyclerView.OnItemTouchListener che ascolta toccare eventi e rileva quando un elemento viene strisciato, l'animazione di conseguenza. Un SwipeListener che viene chiamato per sapere se un oggetto può essere rimosso e richiamato quando gli oggetti vengono respinti. Per usarlo, copiare lo SwipeableRecyclerViewTouchListener classe al progetto e utilizzarlo come questo

mAdapter = new CardViewAdapter(mItems); 

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); 

    mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    mRecyclerView.setAdapter(mAdapter); 

    SwipeableRecyclerViewTouchListener swipeTouchListener = 
      new SwipeableRecyclerViewTouchListener(mRecyclerView, 
        new SwipeableRecyclerViewTouchListener.SwipeListener() { 
         @Override 
         public boolean canSwipe(int position) { 
          return true; 
         } 

         @Override 
         public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) { 
          for (int position : reverseSortedPositions) { 
           mItems.remove(position); 
           mAdapter.notifyItemRemoved(position); 
          } 
          mAdapter.notifyDataSetChanged(); 
         } 

         @Override 
         public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) { 
          for (int position : reverseSortedPositions) { 
           mItems.remove(position); 
           mAdapter.notifyItemRemoved(position); 
          } 
          mAdapter.notifyDataSetChanged(); 
         } 
        }); 

    mRecyclerView.addOnItemTouchListener(swipeTouchListener); 
} 

Questo può anche essere utile: http://www.getgoodcode.com/2013/06/swipe-to-dismiss-google-style/

auguro che funziona per voi!

Problemi correlati