Slay
Slay

Reputation: 740

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

<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.

Upvotes: 14

Views: 6822

Answers (2)

brnunes
brnunes

Reputation: 1953

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

Upvotes: 13

tasomaniac
tasomaniac

Reputation: 10342

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.

Upvotes: 1

Related Questions