由于項目上的需要側(cè)滑條目展示收藏按鈕,記得之前代碼家有寫過一個厲害的開源控件 AndroidSwipeLayout 本來準(zhǔn)備直接拿來使用,但是看過 issue 發(fā)現(xiàn)現(xiàn)在有不少使用者反應(yīng)有不少的 bug ,而且代碼家現(xiàn)在貌似也不進(jìn)行維護(hù)了.故自己實現(xiàn)了一個所要效果的一個控件.因為只是實現(xiàn)我需要的效果,所以大家也能看到,代碼里有不少地方我是寫死的.希望對大家有些幫助.而且暫時也不需要 AndroidSwipeLayout 大而全的功能,算是變相給自己做的項目精簡代碼了.
完整示例代碼請看:GitHub 地址
主要源碼:
public class SwipeLayout extends FrameLayout {public static final int CLOSE = 0;public static final int OPEN = 1;private int mState = CLOSE;private int mWidth;private int mHeight;private float mDownX;private float mDownY;private SwipeListener mSwipeListener;private View mTopView;private View mBottomView;private ViewDragHelper mViewDragHelper;public SwipeLayout(Context context) {this(context, null);}public SwipeLayout(Context context, AttributeSet attrs) {this(context, attrs, 0);}public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mViewDragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {@Overridepublic boolean tryCaptureView(View child, int pointerId) {//只對mTopView進(jìn)行處理return child == mTopView;}@Overridepublic int clampViewPositionHorizontal(View child, int left, int dx) {//設(shè)置橫向滑動的邊界(left的值是mTopView左上角點的x坐標(biāo)值)int newLeft;if (left <= -mBottomView.getMeasuredWidth()) {newLeft = -mBottomView.getMeasuredWidth();} else if (left >= 0) {newLeft = 0;} else {newLeft = left;}return newLeft;}@Overridepublic int clampViewPositionVertical(View child, int top, int dy) {//因為不需要上下的滑動直接設(shè)置為0(top的值是mTopView左上角點的y坐標(biāo)值)return 0;}@Overridepublic void onViewReleased(View releasedChild, float xvel, float yvel) {//手指松開時會回調(diào)該函數(shù)int right = mWidth - releasedChild.getRight();//mTopView右邊界距離屏幕右邊的距離int bottomWidth = mBottomView.getMeasuredWidth();if (right > bottomWidth * 9 / 10) {scrollToLeftEdge();return;}if (right <= bottomWidth / 10 && right > 0) {scrollToRightEdge();return;}if (xvel == 0) {//速度為0時單獨(dú)處理if (right >= bottomWidth / 2) {scrollToLeftEdge();} else if (right < bottomWidth / 2) {scrollToRightEdge();}return;}if (xvel > 0) {//向右滑動后松手scrollToRightEdge();} else {//向左滑動后松手scrollToLeftEdge();}}});}@Overridepublic void computeScroll() {if (mViewDragHelper.continueSettling(true)) {invalidate();}}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:mDownX = ev.getRawX();mDownY = ev.getRawY();if (mState == CLOSE) {return true;}break;case MotionEvent.ACTION_MOVE:float distanceX = ev.getRawX() - mDownX;float distanceY = ev.getRawY() - mDownY;float angle;if (distanceX == 0) {angle = 90;} else {angle = (float) Math.toDegrees(Math.atan(Math.abs(distanceY / distanceX)));}if (angle < 45) {return true;//攔截事件交給自己處理滑動}break;}return mViewDragHelper.shouldInterceptTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {ViewParent viewParent = getParent();switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:mDownX = ev.getRawX();mDownY = ev.getRawY();break;case MotionEvent.ACTION_MOVE:float distanceX = ev.getRawX() - mDownX;float distanceY = ev.getRawY() - mDownY;float angle;if (distanceX == 0) {angle = 90;} else {angle = (float) Math.toDegrees(Math.atan(Math.abs(distanceY / distanceX)));}if (angle < 45 && viewParent != null) {viewParent.requestDisallowInterceptTouchEvent(true);//讓父控件不要處理事件,交給子控件}break;case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_UP:if (viewParent != null) {viewParent.requestDisallowInterceptTouchEvent(false);}break;}mViewDragHelper.processTouchEvent(ev);return true;}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);int measureHeight = mBottomView.getMeasuredHeight();int measureWidth = mBottomView.getMeasuredWidth();mBottomView.layout(mWidth - measureWidth, (mHeight - measureHeight) / 2, mWidth, mHeight + measureHeight / 2);//靠右邊界垂直居中if (mState == OPEN) {mTopView.layout(-measureWidth, 0, mTopView.getMeasuredWidth() - measureWidth, mTopView.getMeasuredHeight());}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);mWidth = w;mHeight = h;}@Overrideprotected void onFinishInflate() {super.onFinishInflate();if (getChildCount() != 2) {throw new IllegalStateException("only and should contain two child view");}View bottomView = getChildAt(0);if (!(bottomView instanceof ViewGroup)) {throw new IllegalStateException("sideslip menu should be contained by a viewgroup");}mBottomView = bottomView;mTopView = getChildAt(1);}//回滾到左邊(只能在onViewReleased里使用該方法)private void scrollToLeftEdge() {mViewDragHelper.settleCapturedViewAt(-mBottomView.getMeasuredWidth(), 0);invalidate();mState = OPEN;if (mSwipeListener != null) {mSwipeListener.onOpenListener(this);}}//回滾到右邊(只能在onViewReleased里使用該方法)private void scrollToRightEdge() {mViewDragHelper.settleCapturedViewAt(0, 0);invalidate();mState = CLOSE;}public void smoothClose() {mViewDragHelper.smoothSlideViewTo(mTopView, 0, 0);invalidate();mState = CLOSE;}public int getState() {return mState;}public void setState(int state) {mState = state;invalidate();}public interface SwipeListener {void onOpenListener(SwipeLayout swipeLayout);}public void setSwipeListener(SwipeListener mSwipeListener) {this.mSwipeListener = mSwipeListener;}}效果圖

以上所述是小編給大家介紹的Android 中 SwipeLayout一個展示條目底層菜單的側(cè)滑控件源碼解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答
圖片精選