使用 SwipeDismissBehavior

SwipeDismissBehavior 適用於任何檢視,並使用 CoordinatorLayout 實現滑動功能以在我們的佈局中消除。

只需使用:

        final SwipeDismissBehavior<MyView> swipe = new SwipeDismissBehavior();

        //Sets the swipe direction for this behavior.
        swipe.setSwipeDirection(
            SwipeDismissBehavior.SWIPE_DIRECTION_ANY);

        //Set the listener to be used when a dismiss event occurs
        swipe.setListener(
            new SwipeDismissBehavior.OnDismissListener() {
            @Override public void onDismiss(View view) {
                //......
            }

            @Override 
            public void onDragStateChanged(int state) {
                //......
            }
        });

        //Attach the SwipeDismissBehavior to a view
        LayoutParams coordinatorParams = 
            (LayoutParams) mView.getLayoutParams();    
        coordinatorParams.setBehavior(swipe);