How can I make my SwipeableCardViews
more like the IOS 7 mail app (swipe to show buttons)
So far I have created an android application which allows the user
I've just done this. This is how it looks like:
I don't have any CardView
but you can have any layout (so you can add the support lib CardView
or whatever you like).
This is how I did it. I used this library. Since the library has minSdkVersion
15 and we have 14, I copy-pasted the following classes directly from GitHub: OnItemClickListener
, RecyclerViewAdapter
, SwipeableItemClickListener
, SwipeToDismissTouchListener
and ViewAdapter
(this are the only ones you need if you use a RecyclerView
).
Because the lib has not been updated yet, you'll need to add the following method into SwipeableItemClickListener
:
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
Your list item layout should have the following structure:
Then simply add this code to your Activity/Fragment
after creating the RecyclerView
and setting it's adapter:
final SwipeToDismissTouchListener touchListener =
new SwipeToDismissTouchListener<>(
new RecyclerViewAdapter(mRecyclerView),
new SwipeToDismissTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
return true;
}
@Override
public void onDismiss(RecyclerViewAdapter recyclerView, int position) {
// remove item at position and notify adapter
}
}
);
mRecyclerView.setOnTouchListener(touchListener);
mRecyclerView.addOnScrollListener((RecyclerView.OnScrollListener) touchListener.makeScrollListener());
mRecyclerView.addOnItemTouchListener(new SwipeableItemClickListener(
getActivity(),
new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if (view.getId() == R.id.swipe_delete) {
touchListener.processPendingDismisses();
} else {
touchListener.undoPendingDismiss();
}
}
}
));