Friday, July 1, 2016

android - CoordinatorLayout SwipeDismissBehavior example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:fitsSystemWindows="true"
    android:background="#b3d0e7"
    >
    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SWIPE ME"
        android:textColor="#fff"
        android:gravity="center"
        android:padding="30dp"
        android:layout_margin="5dp"
        android:background="#ff0000"
        android:layout_gravity="center"
        />
    <TextView
        android:id="@+id/tv_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#5e71b7"
        android:padding="10dp"
        android:textSize="20sp"
        android:textColor="#c8d5fd"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.SwipeDismissBehavior;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.support.design.widget.CoordinatorLayout.LayoutParams;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private TextView mTVCenter;
    private TextView mTVInfo;

    private SwipeDismissBehavior<TextView> mSwipe;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mTVCenter = (TextView) findViewById(R.id.tv_center);
        mTVInfo = (TextView) findViewById(R.id.tv_info);

        // Initialize a new SwipeDismissBehavior
        mSwipe = new SwipeDismissBehavior<>();

        // Define the swipe direction
        mSwipe.setSwipeDirection(SwipeDismissBehavior.SWIPE_DIRECTION_ANY);
        //mSwipe.setSwipeDirection(SwipeDismissBehavior.SWIPE_DIRECTION_START_TO_END);
        //mSwipe.setSwipeDirection(SwipeDismissBehavior.SWIPE_DIRECTION_END_TO_START);

        // Set a listener for SwipeDismissBehavior
        mSwipe.setListener(new SwipeDismissBehavior.OnDismissListener() {
            @Override
            public void onDismiss(View view) {
                Snackbar.make(
                       mCLayout,
                        "View Swiped.",
                        Snackbar.LENGTH_LONG
                ).show();

                mTVInfo.setText("View Swiped.");
            }

            @Override
            public void onDragStateChanged(int state) {
                switch(state){

                    case SwipeDismissBehavior.STATE_DRAGGING:
                        mTVCenter.setBackgroundColor(Color.parseColor("#FF8CAEA1"));
                        mTVInfo.setText("STATE DRAGGING");
                        break;

                    case SwipeDismissBehavior.STATE_IDLE:
                        mTVCenter.setBackgroundColor(Color.parseColor("#FF979797"));
                        mTVInfo.setText("STATE IDLE");
                        break;

                    case SwipeDismissBehavior.STATE_SETTLING:
                        mTVCenter.setBackgroundColor(Color.parseColor("#FF595CA9"));
                        mTVInfo.setText("STATE SETTLING");
                        break;

                    default:
                        mTVCenter.setBackgroundColor(Color.BLUE);
                        mTVInfo.setText("STATE DEFAULT");
                }
            }
        });

        // Get the layout parameters for TextView
        LayoutParams layoutParams = (LayoutParams) mTVCenter.getLayoutParams();

        // Set the SwipeDismissBehavior to the TextView
        layoutParams.setBehavior(mSwipe);
    }
}