Swipe Dismiss with Coordinate layout
CoordinatorLayout
Published in
1 min readJul 29, 2016
- CoordinatorLayout is a super-powered FrameLayout.
- It is intended for two primary use cases:
- As a top-level application decor or chrome layout
- As a container for a specific interaction with one or more child views
To I am going to implement how to do a swipe dismiss the Cardview with Coordinate Layout
Preparing for Swipe Dismiss
- CoordinateLayout
- CardView
- SwipeDismissBehavior Class
CardView
A FrameLayout with a rounded corner background and shadow.
SwipeDismissBehavior
An interaction behavior plugin for child views of CoordinatorLayout to provide support for the ‘swipe-to-dismiss’ gesture.
Now Implementation
layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.swipedismissapplication.SwipeDismissActivity">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cl_swipedismiss"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.CardView
android:id="@+id/cv_swipedismiss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30dp"
/>
</android.support.v7.widget.CardView>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
Activity.java
public class SwipeDismissActivity extends AppCompatActivity {
private CardView cvSwipeDismiss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_dismiss);
setUpViews();
swipeDismiss();
}
private void swipeDismiss() {
//<V> - The View type that this Behavior operates on SwipeDismissBehavior<CardView> swipeDismissBehavior=new SwipeDismissBehavior();
swipeDismissBehavior.setSwipeDirection(SwipeDismissBehavior.SWIPE_DIRECTION_ANY );
CoordinatorLayout.LayoutParams layoutParams =
(CoordinatorLayout.LayoutParams) cvSwipeDismiss.getLayoutParams();
layoutParams.setBehavior(swipeDismissBehavior);
}
private void setUpViews() {
cvSwipeDismiss = (CardView) findViewById(R.id.cv_swipedismiss);
}
}