5

मैं अपने प्रत्येक कार्डव्यू के स्वाइप इशारा करने के लिए FrameLayout का उपयोग कर रहा हूं, लेकिन कार्ड हमेशा खारिज नहीं होते हैं। और कभी-कभी वे बाईं ओर दाईं ओर लटकाते हैं जब तक कि उपयोगकर्ता दूसरी बार कार्ड को स्वाइप नहीं करता।मेरे कार्डव्यू हमेशा बाएं या दाएं स्वाइप किए जाने पर खारिज नहीं होंगे?

मैं इसे कैसे ठीक कर सकता हूं?

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

क्या आपके पास अपनी एक्सएमएल शो के रूप में एक सूची है या केवल एक ही कार्ड व्यू है? –

उत्तर

0

मैं romannurik के एंड्रायड-SwipeToDismiss अनुकूलित वास्तव में ऐसा करने के लिए।

कोड एक वर्किंग नमूना आवेदन के साथ GitHub पर है, और के होते हैं:

RecyclerView.OnItemTouchListener का एक उपवर्ग है कि घटनाओं को छूने के लिए सुनता है और जब एक आइटम स्वाइप किया जा रहा है का पता लगाता है, यह उसके अनुसार एनिमेट। एक स्वाइप लिस्टनर जिसे यह जानने के लिए बुलाया जाता है कि आइटम को खारिज कर दिया जा सकता है और आइटम को खारिज करते समय फिर से बुलाया जाता है। इसके इस्तेमाल के लिये अपनी परियोजना के लिए कक्षा SwipeableRecyclerViewTouchListener कॉपी और इस

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

की तरह उपयोग यह भी उपयोगी हो सकता है: http://www.getgoodcode.com/2013/06/swipe-to-dismiss-google-style/

यह आप के लिए काम करता है आशा है!

संबंधित मुद्दे