You will have to put it inside the RecyclerView (and your CardView as the only element there)
Then use ItemTouchHelper.SimpleCallback to itemTouchHelper.attachToRecyclerView(recyclerView);
This will give you animation in
@Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) { }
You can specify a specific action depending on the direction of movement.
See full instructions here: Swipe the screen for the RecyclerView
In addition, you need to disable vertical scrolling in RecyclerView :
public class UnscrollableLinearLayoutManager extends LinearLayoutManager { public UnscrollableLinearLayoutManager(Context context) { super(context); } @Override public boolean canScrollVertically() { return false; } } ..... RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new UnscrollableLinearLayoutManager(this)); recyclerView.setAdapter(new RestaurantCardAdapter());
Otherwise, as soon as you try to scroll up or down, you will see an animation of the end t2> end-of-list.
Upd:
Here's the RecyclerView.Adapter I used for the test:
private class RestaurantCardAdapter extends RecyclerView.Adapter { @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new RestaurantViewHolder(new RestaurantCard(parent.getContext())); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {} @Override public int getItemCount() { return 1; } private class RestaurantViewHolder extends RecyclerView.ViewHolder { public RestaurantViewHolder(View itemView) { super(itemView); } } }
RestaurantCard is a regular View (extends CardView in our case):
public class RestaurantCard extends CardView { public RestaurantCard(Context context) { super(context); initialize(context); } public RestaurantCard(Context context, AttributeSet attrs) { super(context, attrs); initialize(context); } private void initialize(Context context){ LayoutInflater.from(context).inflate(R.layout.card, this);