2014-10-17 40 views
13

您好我正在使用支持庫android.support.v7.widget.CardView來實現我的android應用程序中的cardview功能。我想實現輕掃以刪除它的功能。如何在Android中使用支持庫刷卡刪除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,請檢查此庫。它使用了一個onSwipeDismissListener,它可能有幫助。 – AmaJayJB 2014-10-17 11:12:47

+0

@AmaJayJB true - 但這是minSDK 14 - 支持更低.. – ligi 2014-10-17 11:18:33

+0

@AmaJayJB我沒有使用該庫。我正在使用cardview概念,谷歌推出支持庫 – 2014-10-17 13:18:49

回答

29

我改編了romannurik's Android-SwipeToDismiss來做到這一點。

The code is on github與沃示例應用程序,和由以下組成:

  • RecyclerView.OnItemTouchListener子類偵聽觸摸事件並檢測當一個項目被偷走,因此動畫它。
  • A 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 2015-01-11 08:22:21

+0

如何刪除一個元素?看起來你沒有參考你滑動的適配器/元素來移除它。 – russellhoff 2015-01-12 11:47:39

+0

解決!只需調用適配器並在for循環中刪除所需的元素即可。 – russellhoff 2015-01-12 12:24:26

8

沒有爲刷卡一種新的方法在支持Android刪除手勢v7 API。 班級名稱爲ItemTouchHelper

Paul Burke先生寫了一個關於如何實現此功能的驚人示例。 看到這個link

+0

驗證了這一點。也許增加一些最小的示例代碼來改進這個答案? – 2016-06-15 03:46:37