5

Tôi đang sử dụng FrameLayout để thực hiện cử chỉ vuốt của mỗi lần xem thẻ của tôi, nhưng thẻ không phải lúc nào cũng loại bỏ. Và đôi khi họ chỉ treo bên trái hoặc bên phải cho đến khi người dùng vuốt thẻ lần thứ hai.Xem trước thẻ của tôi sẽ không luôn bị loại bỏ khi được vuốt sang trái hoặc phải?

Làm cách nào để khắc phục sự cố này?

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

Bạn có một danh sách hoặc chỉ một ThẻView duy nhất làm các chương trình xml của bạn không? –

Trả lời

0

tôi thích Android SwipeToDismiss romannurik để làm chính xác điều đó.

Mã này là trên github với một ứng dụng mẫu woking, và bao gồm:

Một lớp con của RecyclerView.OnItemTouchListener mà nghe chạm vào các sự kiện và phát hiện khi một mục đã được swiped, hiệu ứng động nó cho phù hợp. Một JamListener được gọi để biết một mục có thể được loại bỏ và được gọi lại khi các mục bị loại bỏ. Để sử dụng nó, sao chép SwipeableRecyclerViewTouchListener lớp để dự án của bạn và sử dụng nó như thế này

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

này cũng có thể hữu ích: http://www.getgoodcode.com/2013/06/swipe-to-dismiss-google-style/

Hy vọng nó sẽ làm việc cho bạn!

Các vấn đề liên quan