使用 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);