實現(xiàn)Activity滑動退出
很多應用在二級詳情頁面加入了滑動退出activity的效果宰闰,很方便茬贵,心血來潮,想著自己也來實現(xiàn)這個效果移袍,就當做練手吧解藻。
實現(xiàn)View的滑動有很多種方法,如自己在onTouchEvent中處理觸摸事件葡盗,然后滾動View到相應位置螟左,也可以用google V4包為我們提供的ViewDragHelper來處理觸摸事件,我們這里選擇后者觅够,因為滑動退出操作都是在屏幕的邊緣時觸發(fā)胶背,而ViewDragHelper剛好提供了想要的實現(xiàn),可以說利用ViewDragHelper來實現(xiàn)我們的需求非常簡單喘先。
先定義一個ViewGroup钳吟,并做一些必要的變量聲明
BaseSwipeLayout
public class BaseSwipeLayout extends FrameLayout{
private View mDragView;
private ViewDragHelper mViewDragHelper;
private Point mAutoBackOrignalPoint = new Point();
private Point mCurArrivePoint = new Point();
private int mCurEdgeFlag = ViewDragHelper.EDGE_LEFT;
private int mSwipeEdge = ViewDragHelper.EDGE_LEFT;
public BaseSwipeLayout(Context context) {
this(context, null);
}
public BaseSwipeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BaseSwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
}
既然使用ViewDragHelper,我們把觸摸事件交給ViewDragHelper處理苹祟,ViewDragHelper不熟悉的同學砸抛,相關知識评雌,網(wǎng)上一大堆,自行查看
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mViewDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mViewDragHelper.processTouchEvent(event);
return true;
}
下面對ViewDragHelper的配置直焙,比較簡單景东,大家看代碼吧。
private void init() {
mViewDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return false;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
mCurArrivePoint.x = left;
//允許左右觸發(fā)滑動奔誓,否則return 0
if (mCurEdgeFlag != ViewDragHelper.EDGE_BOTTOM) {
return left;
}else return 0;
}
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
mCurArrivePoint.y = top;
//允許底部觸發(fā)滑動斤吐,否則return 0
if (mCurEdgeFlag == ViewDragHelper.EDGE_BOTTOM) {
return top;
}else return 0;
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
switch (mCurEdgeFlag) {
case ViewDragHelper.EDGE_LEFT:
//水平滑動超過一半,觸發(fā)結束
if (mCurArrivePoint.x > getWidth()/2) {
mViewDragHelper.settleCapturedViewAt(getWidth(), mAutoBackOrignalPoint.y);
}else {
mViewDragHelper.settleCapturedViewAt(mAutoBackOrignalPoint.x, mAutoBackOrignalPoint.y);
}
break;
case ViewDragHelper.EDGE_RIGHT:
//水平滑動超過一半厨喂,觸發(fā)結束
if (mCurArrivePoint.x < -getWidth()/2) {
mViewDragHelper.settleCapturedViewAt(-getWidth(), mAutoBackOrignalPoint.y);
}else {
mViewDragHelper.settleCapturedViewAt(mAutoBackOrignalPoint.x, mAutoBackOrignalPoint.y);
}
break;
case ViewDragHelper.EDGE_BOTTOM:
//垂直滑動超過一半和措,觸發(fā)結束
if (mCurArrivePoint.y < -getHeight()/2) {
mViewDragHelper.settleCapturedViewAt(mAutoBackOrignalPoint.x, -getHeight());
}else {
mViewDragHelper.settleCapturedViewAt(mAutoBackOrignalPoint.x, mAutoBackOrignalPoint.y);
}
break;
}
mCurArrivePoint.x = 0;
mCurArrivePoint.y = 0;
invalidate();
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
super.onViewPositionChanged(changedView, left, top, dx, dy);
switch (mCurEdgeFlag) {
case ViewDragHelper.EDGE_LEFT:
if (left >= getWidth()) {
if (mFinishScroll != null) {
mFinishScroll.complete();
}
}
break;
case ViewDragHelper.EDGE_RIGHT:
if (left <= -getWidth()) {
if (mFinishScroll != null) {
mFinishScroll.complete();
}
}
break;
case ViewDragHelper.EDGE_BOTTOM:
if (top <= -getHeight()) {
if (mFinishScroll != null) {
mFinishScroll.complete();
}
}
break;
}
}
@Override
public void onEdgeDragStarted(int edgeFlags, int pointerId) {
mCurEdgeFlag = edgeFlags;
if (mDragView == null) mDragView = getChildAt(0);
mViewDragHelper.captureChildView(mDragView, pointerId);
}
});
mViewDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
}
這里還有個步驟不要忘了,手指離開屏幕或者滑動超過屏幕的時候蜕煌,我們觸發(fā)了ViewGroup自行完全滾動出屏幕的調(diào)用派阱,所以我們需要在computeScroll中做檢查,如果滾動沒有結束斜纪,刷新View贫母,繼續(xù)滾動。
@Override
public void computeScroll() {
if (mViewDragHelper.continueSettling(true)) {
invalidate();
}
}
這樣我們自定義的BaseSwipeLayout打造完畢盒刚,我們把它設為activity的根布局測試一下
<?xml version="1.0" encoding="utf-8"?>
<com.aliouswang.swipeback.widget.BaseSwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="@+id/swipe_layout"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Swipe Back Demo"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@android:color/holo_red_light"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:text="Swipe Back Demo"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_light"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:text="Swipe Back Demo"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@android:color/holo_green_light"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:text="Swipe Back Demo"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@android:color/holo_purple"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:text="Swipe Back Demo"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@android:color/holo_green_dark"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:text="Swipe Back Demo"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@android:color/holo_red_dark"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:text="Swipe Back Demo"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_dark"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:text="Swipe Back Demo"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@android:color/holo_orange_dark"
android:layout_width="match_parent"
android:layout_height="100dp" />
</LinearLayout>
</ScrollView>
</com.aliouswang.swipeback.widget.BaseSwipeLayout>
當然腺劣,我們已經(jīng)實現(xiàn)了整個功能,但是有一點很不爽的是因块,我們必須將BaseSwipeLayout作為布局的根橘原,這樣實現(xiàn)還不夠優(yōu)雅,我們能不能不改變我們原有的布局文件涡上,卻依然能加入滑動退出功能趾断。
這里我們要介紹一個DecorView,它是Window的最頂層View吓懈,它含有一個子LinearLayout歼冰,代表整個Window,包括通知欄耻警,狀態(tài)欄,內(nèi)容顯示區(qū)域甸怕,所以我們activity頁面是DecorView的子View的子View甘穿,那么我們能不能直接給DecorView的子View添加到我們的BaseSwipeLayout,再將BaseSwipeLayout添加到DecorView梢杭,當然是可以的温兼,而且這種方式,我們不需要改變原來的布局文件武契,更加優(yōu)雅募判。
我們定義一個SwipeHelper類荡含,輔助我們進行BaseSwipeLayout插入操作。
public class SwipeHelper {
private Activity mActivity;
private BaseSwipeLayout mBaseSwipeLayout;
public SwipeHelper(Activity activity) {
this.mActivity = activity;
}
public void onActivityCreate() {
mBaseSwipeLayout = (BaseSwipeLayout) LayoutInflater.from(mActivity)
.inflate(R.layout.swipe_layout, null);
mBaseSwipeLayout.setOnFinishScroll(new BaseSwipeLayout.OnFinishScroll() {
@Override
public void complete() {
mActivity.finish();
}
});
}
public void onPostCreate() {
mBaseSwipeLayout.attachToActivity(mActivity);
}
public void setSwipeEdge(int edgeFlag) {
mBaseSwipeLayout.setSwipeEdge(edgeFlag);
}
}
BaseSwipeLayout
//核心代碼届垫,綁定到相應activity
public void attachToActivity(Activity activity) {
this.mActivity = activity;
TypedArray a = activity.getTheme().obtainStyledAttributes(new int[]{
android.R.attr.windowBackground
});
int background = a.getResourceId(0, 0);
a.recycle();
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
ViewGroup decorChild = (ViewGroup) decorView.getChildAt(0);
decorChild.setBackgroundResource(background);
decorView.removeView(decorChild);
addView(decorChild);
decorView.addView(this);
}
需要添加滑動退出的activity释液,添加想要代碼
private SwipeHelper mSwipeHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_drag_helper_act_layout);
mSwipeHelper = new SwipeHelper(this);
mSwipeHelper.onActivityCreate();
mSwipeHelper.setSwipeEdge(ViewDragHelper.EDGE_RIGHT);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mSwipeHelper.onPostCreate();
}
到此我們的代碼基本完成,源碼我放到了BaseSwipe装处,歡迎指教误债!