2014-10-17 14 views
13

का उपयोग कर एंड्रॉइड में कार्डव्यू हटाने के लिए स्वाइप कैसे करें हाय मैं अपने एंड्रॉइड ऐप में कार्डव्यू कार्यक्षमता प्राप्त करने के लिए समर्थन लाइब्रेरी android.support.v7.widget.CardView का उपयोग कर रहा हूं। मैं इसमें कार्यक्षमता को हटाने के लिए स्वाइप प्राप्त करना चाहता हूं।समर्थन पुस्तकालय

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_gravity="center" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    card_view:cardCornerRadius="4dp"> 

    <TextView 
     android:id="@+id/info_text" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</android.support.v7.widget.CardView> 

इसमें कार्यक्षमता को हटाने के लिए स्वाइप कैसे प्राप्त करें?

अग्रिम धन्यवाद।

+0

https://github.com/gabrielemariotti/cardslib, इस लाइब्रेरी को देखें। यह एक ऑनस्वाइडिस्मिस्लिस्टर का उपयोग करता है जो मदद कर सकता है। – AmaJayJB

+0

@AmaJayJB सच - लेकिन यह minSDK 14 है - समर्थन कम है .. – ligi

+0

@AmaJayJB मैं उस पुस्तकालय का उपयोग नहीं कर रहा हूं। मैं कार्डव्यू अवधारणा का उपयोग कर रहा हूं कि Google ने समर्थन पुस्तकालय में पेश किया –

उत्तर

29

मैंने romannurik's Android-SwipeToDismiss को ठीक से करने के लिए अनुकूलित किया।

एक वर्किंग नमूना आवेदन के साथ The code is on github, और के होते हैं:

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

इसका इस्तेमाल, GitHub पर निर्देशों का पालन करें, या सिर्फ अपनी परियोजना के लिए कक्षा SwipeableRecyclerViewTouchListener कॉपी और इस तरह इसका इस्तेमाल करने के लिए:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mItems = new ArrayList<>(30); 
    for (int i = 0; i < 30; i++) { 
     mItems.add(String.format("Card number %2d", i)); 
    } 

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

बहुत बढ़िया! एक आकर्षण की तरह काम करता है, धन्यवाद! – Luis

+0

कोई तत्व कैसे हटाएं? ऐसा लगता है कि आपके पास इसे हटाने के लिए स्वाइप करने वाले एडाप्टर/तत्व का कोई संदर्भ नहीं है। – russellhoff

+0

हल हो गया! बस एडाप्टर को कॉल करें और उस लूप के भीतर इच्छित तत्व को हटा दें। – russellhoff

8

कड़ी चोट के लिए एक नया दृष्टिकोण एंड्रॉयड समर्थन में इशारा हटाना नहीं है वी 7 एपीआई। कक्षा का नाम ItemTouchHelper कहा जाता है।

श्री पॉल बर्क ने इस सुविधा को कार्यान्वित करने के तरीके पर एक अद्भुत उदाहरण लिखा। यह link देखें।

+0

यह सत्यापित किया गया है। शायद इस उत्तर को बेहतर बनाने के लिए कुछ न्यूनतम उदाहरण कोड जोड़ें? –

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