https://github.com/daimajia/AndroidSwipeLayout
侧滑的删除按钮在右边为例分析:
核心函数:
ViewDragHelper //拉动效果的函数。
打开app,初始化view
<?xml version="1.0" encoding="utf-8"?> <com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/godfather" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_gravity="right" android:src="@drawable/bird" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="nihao"/> </com.daimajia.swipe.SwipeLayout>
1. addView ImageView
2. addView TextView
3. 调了两次onLayout
protected void onLayout(boolean changed, int l, int t, int r, int b) {07-20 19:30:37.926 25279-25279/com.daimajia.swipedemo D/SwipeLayout: onLayout() called with: changed = [true], l = [0], t = [0], r = [480], b = [672]
07-20 19:30:37.976 25279-25279/com.daimajia.swipedemo D/SwipeLayout: onLayout() called with: changed = [false], l = [0], t = [0], r = [480], b = [672]
void layoutPullOut() { // 如果两个view,拼成一个view的。 View surfaceView = getSurfaceView(); // TextView Log.d(TAG, "layoutPullOut: surfaceView:"+surfaceView); Rect surfaceRect = mViewBoundCache.get(surfaceView); if(surfaceRect == null) surfaceRect = computeSurfaceLayoutArea(false); //Rect(0, 0 - 480, 672) 左上(0,0)右下(480,672) if (surfaceView != null) { surfaceView.layout(surfaceRect.left, surfaceRect.top, surfaceRect.right, surfaceRect.bottom); bringChildToFront(surfaceView); } View currentBottomView = getCurrentBottomView();//imageview Rect bottomViewRect = mViewBoundCache.get(currentBottomView); if(bottomViewRect == null) bottomViewRect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, surfaceRect);//Rect(480, 0 - 630, 672) if (currentBottomView != null) { currentBottomView.layout(bottomViewRect.left, bottomViewRect.top, bottomViewRect.right, bottomViewRect.bottom); } }
layoutPullout效果就是这样的。
1.
onTouchEvent ACTION_DOWN
按第一下,全局变量记录这个点。
2.滑动:
MotionEvent.ACTION_MOVE
1.判断滑动方向
2.
mDragHelper.processTouchEvent(ev);
clampViewPositionHorizontal
case Right: if (left > getPaddingLeft()) return getPaddingLeft(); if (left < getPaddingLeft() - mDragDistance) return getPaddingLeft() - mDragDistance; break return left;