public DragLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initAttars(context, attrs);
// a.初始化 (通过静态方法)
mDragHelper = ViewDragHelper.create(this, mSensitivity, mCallback);
}
- 3)重写 onInterceptTouchEvent和onTouchevent 方法 ,将事件交给
// b.传递触摸事件
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// 传递给mDragHelper
return mDragHelper.shouldInterceptTouchEvent(ev);
}
/***
-
将事件交给mDragHelper处理
-
@param event
-
@return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
mDragHelper.processTouchEvent(event);
} catch (Exception e) {
e.printStackTrace();
}
// 返回true, 持续接受事件
return true;
}
- 4)重写onFinishInflate方法,在里面拿到 我们的侧滑菜单mLeftContent和主菜单mMainContent
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// 容错性检查 (至少有俩子View, 子View必须是ViewGroup的子类)
if (getChildCount() < 2) {
throw new IllegalStateException("布局至少有俩孩子. Your ViewGroup must have 2 children at " +
“least.”);
}
if (!(getChildAt(0) instanceof ViewGroup && getChildAt(1) instanceof ViewGroup)) {
throw new IllegalArgumentException("子View必须是ViewGroup的子类. Your children must be an " +
“instance of ViewGroup”);
}
mLeftContent = (ViewGroup) getChildAt(0);
mMainContent = (ViewGroup) getChildAt(1);
}
下面我们一起来看一下这个mCallBack是什么东西
看之前我们需要了解Status和OnDragStatusChangeListener这两个东西。
-
Status代表DrawLayout 当前的状态,是否是打开,关闭还是拖拽。
-
OnDragStatusChangeListener是个监听器,在DrawLayout状态改变的时候会回调相关的方法,方便与外界进行通讯。
-
我们可以通过 setDragStatusListener(OnDragStatusChangeListener mListener);这个方法设置监听
/**
- 状态枚举
*/
public static enum Status {
Close, Open, Draging;
}
/**
- 抽屉开关的监听器
*/
public interface OnDragStatusChangeListener {
void onClose();
void onOpen();
void onDraging(float percent);
}
接下来我们来看ViewDragHelper.Callback几个主要的方法
tryCaptureView(View child, int pointerId)
Called when the user’s input indicates that they want to capture the given child view with the pointer indicated by pointerId.
onViewCaptured(View capturedChild, int activePointerId)
Called when a child view is captured for dragging or settling.
getViewHorizontalDragRange(View child)
Return the magnitude of a draggable child view’s horizontal range of motion in pixels.
clampViewPositionHorizontal(View child, int left, int dx)
Restrict the motion of the dragged child view along the horizontal axis.
onViewPositionChanged(View changedView, int left, int top, int dx, int dy)
Called when the captured view’s position changes as the result of a drag or settle.
onViewReleased(View releasedChild, float xvel, float yvel)
Called when the child view is no longer being actively dragged.
谷歌官方的连接;[https://developer.android.com/reference/android/support/v4/widget/ViewDragHelper.Callback.html](()
下面的代码有关于这几个方法的中文解释,这里就不详细讲解了
ViewDragHelper.Callback mCallback = new ViewDragHelper.Callback() {
// d. 重写事件
// 1. 根据返回结果决定当前child是否可以拖拽
// child 当前被拖拽的View
// pointerId 区分多点触摸的id
@Override
public boolean tryCaptureView(View child, int pointerId) {
Log.d(TAG, "tryCaptureView: " + child);
return mToogle;
}
@Override
public void onViewCaptured(View capturedChild, int activePointerId) {
Log.d(TAG, "onViewCaptured: " + capturedChild);
// 当capturedChild被捕获时,调用.
super.onViewCaptured(capturedChild, activePointerId);
}
@Override
public int getViewHorizontalDragRange(View child) {
// 返回拖拽的范围, 不对拖拽进行真正的限制. 仅仅决定了动画执行速度
Log.i(TAG, “getViewHorizontalDragRange:mRange=” +mRange);
return mRange;
}
// 2. 根据建议值 修正将要移动到的(横向)位置 (重要)
// 此时没有发生真正的移动
public int clampViewPositionHorizontal(View child, int left, int dx) {
// child: 当前拖拽的View
// left 新的位置的建议值, dx 位置变化量
// left = oldLeft + dx;
Log.d(TAG, "clampViewPositionHorizontal: "
- "oldLeft: " + child.getLeft() + " dx: " + dx + " left: " + left);
if (child == mMainContent) {
left = fixLeft(left);
}
return left;
}
// 3. 当View位置改变的时候, 处理要做的事情 (更新状态, 伴随动画, 重绘界面)
// 此时,View已经发生了位置的改变
@Override
public void onViewPositionChanged(View changedView, int left, int top,
int dx, int dy) {
// changedView 改变位置的View
// left 新的左边值
// dx 水平方向变化量
super.onViewPositionChanged(changedView, left, top, dx, dy);
Log.d(TAG, "onViewPositionChanged: " + "left: " + left + " dx: " + dx);
int newLeft = left;
if (changedView == mLeftContent) {
// 把当前变化量传递给mMainContent
newLeft = mMainContent.getLeft() + dx;
}
// 进行修正
newLeft = fixLeft(newLeft);
if (changedView == mLeftContent) {
// 当左面板移动之后, 再强制放回去.
mLeftContent.layout(0, 0, 0 + mWidth, 0 + mHeight);
mMainContent.layout(newLeft, 0, newLeft + mWidth, 0 + mHeight);
}
// 更新状态,执行动画
dispatchDragEvent(newLeft);
// 为了兼容低版本, 每次修改值之后, 进行重绘
invalidate();
}
// 4. 当View被释放的时候, 处理的事情(执行动画)
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
// View releasedChild 被释放的子View
// float xvel 水平方向的速度, 向右为+
// float yvel 竖直方向的速度, 向下为+
Log.d(TAG, "onViewReleased: " + "xvel: " + xvel + " yvel: " + yvel);
super.onViewReleased(releasedChild, xvel, yvel);
// 判断执行 关闭/开启
// 先考虑所有开启的情况,剩下的就都是关闭的情况
if (xvel == 0 && mMainContent.getLeft() > mRange / 2.0f) {
open();
} else if (xvel > 0) {
open();
} else {
close();
}
}
@Override
public void onViewDragStateChanged(int state) {
// TODO Auto-generated method stub
super.onViewDragStateChanged(state);
}
};
其实主要思路就是
-
1)在方法public boolean tryCaptureView(View child, int pointerId)处理那些child可以被捕捉,这里我们返回true表示所有的都可以被捕捉
-
2)在public int clampViewPositionHorizontal(View child, int left, int dx)方法中根据child返回将要移动的水平位置的偏移量
-
3)在 void onViewPositionChanged(View changedView, int left, int top, int dx, int dy)方法中处理要做的事情 包括更新状态, 伴随动画, 重绘界面等
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
// 进行修正
newLeft = fixLeft(newLeft);
if (changedView == mLeftContent) {
// 当左面板移动之后, 再强制放回去.
mLeftContent.layout(0, 0, 0 + mWidth, 0 + mHeight);
mMainContent.layout(newLeft, 0, newLeft + mWidth, 0 + mHeight);
if (changedView == mLeftContent) {
// 把当前变化量传递给mMainContent
newLeft = mMainContent.getLeft() + dx;
}
}
// 更新状态,执行动画
dispatchDragEvent(new 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 Left);
// 为了兼容低版本, 每次修改值之后, 进行重绘
invalidate();
}
protected void dispatchDragEvent(int newLeft) {
float percent = newLeft * 1.0f / mRange;
//0.0f -> 1.0f
Log.d(TAG, "percent: " + percent);
if (mListener != null) {
mListener.onDraging(percent);
}
// 更新状态, 执行回调
Status preStatus = mStatus;
mStatus = updateStatus(percent);