Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swipe to delete a Card (using appcompat v7's CardView)

Tags:

java

android

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
                                    android:orientation="horizontal"
                                    android:layout_width="match_parent"
                                    android:stateListAnimator="@anim/anim"
                                    android:layout_margin="5dp"
                                    android:clickable="true"
                                    android:layout_height="wrap_content">
    <TextView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/textview"
              android:minHeight="?android:listPreferredItemHeight"
              android:gravity="center_vertical">

    </TextView>
</android.support.v7.widget.CardView>

I'm using CardView to display a row of texts. How do I swipe to delete those rows -- which are cardviews? Also, how to set an onItemClickListener for each row? Again, I'm using cardview to display each row.

like image 738
Slay Avatar asked Oct 28 '14 20:10

Slay


People also ask

How to implement swipe to delete in android?

Android Swipe To Delete. Swipe to delete feature is commonly used to delete rows from a RecyclerView. In order to implement Swipe to delete feature, we need to use the ItemTouchHelper utility class.

What is cardCornerRadius?

card view:cardCornerRadius - This attribute is used to set the corner radius for the CardView. card_view:cardElevation - This attribute is used to set the elevation for the CardView. The elevation is used to show the shadow of the CardView.


2 Answers

I wanted to do something similar, so I adapted romannurik's Android-SwipeToDismiss to do exactly what we wanted.

The code is on github with a working sample application, and consists of:

  • A subclass of RecyclerView.OnItemTouchListener that listens to touch events and detects when an item is being swiped, animating it accordingly.
  • A SwipeListener that is called in order to know if an item can be dismissed and called again when items are dismissed.

To use it, follow the instructions on github, or just copy the class SwipeableRecyclerViewTouchListener to your project and use it like this:

@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);
}
like image 89
brnunes Avatar answered Oct 13 '22 13:10

brnunes


Here is the famous Swipe to Dismiss example from Roman Nurik.

https://github.com/romannurik/Android-SwipeToDismiss

It includes dismissing items in a list and dismissing separate Views. It should work on any View including CardView.

SwipeDismissListViewTouchListener is for using in a ListView to swipe items. SwipeDismissTouchListener is for any View to dismiss the whole View completely.

like image 38
tasomaniac Avatar answered Oct 13 '22 14:10

tasomaniac