由于項目上的需要側(cè)滑條目展示收藏按鈕,記得之前代碼家有寫過一個厲害的開源控件 AndroidSwipeLayout 本來準(zhǔn)備直接拿來使用,但是看過 issue 發(fā)現(xiàn)現(xiàn)在有不少使用者反應(yīng)有不少的 bug ,而且代碼家現(xiàn)在貌似也不進行維護了.故自己實現(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 GestureDetectorCompat mGestureDetector;
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);
mGestureDetector = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
if (mSwipeListener != null) {
mSwipeListener.onClickListener();
}
return super.onSingleTapUp(e);
}
});
mViewDragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {//只對mTopView進行處理
return child == mTopView;
}
@Override
public 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;
}
@Override
public int clampViewPositionVertical(View child, int top, int dy) {//因為不需要上下的滑動直接設(shè)置為0(top的值是mTopView左上角點的y坐標(biāo)值)
return 0;
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {//手指松開時會回調(diào)該函數(shù)
if (xvel == 0 && yvel == 0) {
return;
}
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時單獨處理
if (right >= bottomWidth / 2) {
scrollToLeftEdge();
} else if (right < bottomWidth / 2) {
scrollToRightEdge();
}
return;
}
if (xvel > 0) {//向右滑動后松手
scrollToRightEdge();
} else {//向左滑動后松手
scrollToLeftEdge();
}
}
});
}
@Override
public void computeScroll() {
if (mViewDragHelper.continueSettling(true)) {
invalidate();
}
}
@Override
public 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);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
mGestureDetector.onTouchEvent(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;
}
@Override
protected 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());
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
}
@Override
protected 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;
if (mSwipeListener != null) {
mSwipeListener.onCloseListener(this);
}
}
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);
void onCloseListener(SwipeLayout swipeLayout);
void onClickListener();
}
public void setSwipeListener(SwipeListener mSwipeListener) {
this.mSwipeListener = mSwipeListener;
}
}
效果圖
SwipeLayout.gif
感謝sendtion的反饋