Android滑动页面返回(自定义控件)
今天看了下腾讯微博,看到“赞你的人”可以向右滑动然后直接返回上一级,兴趣一来,抽点空就把这个控件给做了。先看几张效果图,本人不会做gif,亲们能理解就OK了。
话不多说,现在直接贴出源码吧。
首先看下调用方式
package com.fay.backwidget; import android.app.Activity; import android.os.Bundle; import com.fay.backwidget.SlidingBackLinearLayout.FinishCallBack; /** * @version 1.0 * @author Fay * @since 2014/8/28 */ public class SlidingBackActivity extends Activity { private SlidingBackLinearLayout mBackLinearLayout = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); mBackLinearLayout = (SlidingBackLinearLayout) findViewById(R.id.back_linearlayout); mBackLinearLayout.setFinishCallBack(new FinishCallBack() { @Override public void finish() { // TODO Auto-generated method stub SlidingBackActivity.this.finish(); } }); } }
可以看到调用的方式十分简单,这就是我们每一个开发者追求的,高内聚低耦合,下面我把自定义控件贴出来吧。
package com.fay.backwidget; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.WindowManager; import android.view.animation.DecelerateInterpolator; import android.view.animation.Interpolator; import android.widget.LinearLayout; import android.widget.Scroller; /** * @version 1.0 * @author Fay * @since 2014/8/28 */ @SuppressLint("NewApi") public class SlidingBackLinearLayout extends LinearLayout { private int xLastTouchLocation; private int xCurrentTouchLocation; private int detaX; private boolean isClose = false; private int windowWidth ; private Scroller mScroller = null; private Interpolator mInterpolator = null; private FinishCallBack mFinishCallBack = null; public SlidingBackLinearLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub init(context); } public SlidingBackLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(context); } public SlidingBackLinearLayout(Context context) { super(context); // TODO Auto-generated constructor stub init(context); } private void init(Context mContext) { mInterpolator = new DecelerateInterpolator(); mScroller = new Scroller(mContext, mInterpolator); DisplayMetrics mDisplayMetrics = new DisplayMetrics(); WindowManager mWindowManager = (WindowManager) mContext.getSystemService(mContext.WINDOW_SERVICE); mWindowManager.getDefaultDisplay().getMetrics(mDisplayMetrics); windowWidth = mDisplayMetrics.widthPixels; } public interface FinishCallBack{ void finish(); } public void setFinishCallBack(FinishCallBack mFinishCallBack) { this.mFinishCallBack = mFinishCallBack; } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: xLastTouchLocation = (int) event.getX(); return true; case MotionEvent.ACTION_MOVE: xCurrentTouchLocation = (int) event.getX(); detaX = xLastTouchLocation - xCurrentTouchLocation; if (detaX < 0) { if (null != mFinishCallBack) { scrollTo( detaX, 0); } } break; case MotionEvent.ACTION_UP: xCurrentTouchLocation = (int) event.getX(); detaX = xLastTouchLocation - xCurrentTouchLocation; if (detaX < 0) { if (null != mFinishCallBack) { if (- detaX >= windowWidth / 2) { // close isClose = true; startMove( -(windowWidth + detaX)); } else { startMove(- detaX); } } } break; } return super.onTouchEvent(event); } private void startMove(int deta) { mScroller.startScroll(getScrollX(), 0, deta, 0); invalidate(); } @Override public void computeScroll() { // TODO Auto-generated method stub super.computeScroll(); if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } else { if (isClose) { if (null != mFinishCallBack) { mFinishCallBack.finish(); } } } } }
其实就是这么简单,希望大家有所收获。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。