我一直在关注 ViewDragHelper 的这个演练
http://flavienlaurent.com/blog/2013/08/28/each-navigation-drawer-hides-a-viewdraghelper/
我目前正在尝试实现自定义布局,但我遇到了 ViewDragHelper 的问题。我正在尝试使用从右到左(即水平)打开的拖动手柄来获得“抽屉”样式布局。我知道 SliderDrawer 处理此任务,但我的目标只是了解 ViewDragHelper。这是我得到的错误:
09-24 14:27:21.031: E/AndroidRuntime(24002): FATAL EXCEPTION: main
09-24 14:27:21.031: E/AndroidRuntime(24002): java.lang.StackOverflowError
09-24 14:27:21.031: E/AndroidRuntime(24002): at android.widget.TextView.setFrame(TextView.java:4365)
09-24 14:27:21.031: E/AndroidRuntime(24002): at android.view.View.layout(View.java:14293)
09-24 14:27:21.031: E/AndroidRuntime(24002): at com.example.vdh.DragLayout.onLayout(DragLayout.java:91)
09-24 14:27:21.031: E/AndroidRuntime(24002): at android.view.View.layout(View.java:14296)
09-24 14:27:21.031: E/AndroidRuntime(24002): at android.view.ViewGroup.layout(ViewGroup.java:4562)
09-24 14:27:21.031: E/AndroidRuntime(24002): at com.example.vdh.DragLayout.onLayout(DragLayout.java:97)
09-24 14:27:21.031: E/AndroidRuntime(24002): at android.view.View.layout(View.java:14296)
09-24 14:27:21.031: E/AndroidRuntime(24002): at android.view.ViewGroup.layout(ViewGroup.java:4562)
请注意,最后 3 行左右是重复的。我知道这个 onLayout 函数负责运行时异常。这是源代码:
package com.example.vdh;
import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
public class DragLayout extends LinearLayout {
private final ViewDragHelper mDragHelper;
private View mDrawerView;
private View mHandle;
private int mDragRange;
private int mLeft;
public DragLayout(Context context) {
this(context, null);
}
public DragLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DragLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback());
}
@Override
protected void onFinishInflate() {
mHandle = findViewById(R.id.handle);
mDrawerView = findViewById(R.id.drawer);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
mDragHelper.cancel();
return false;
}
return mDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
mDragHelper.processTouchEvent(ev);
return true;
}
private class DragHelperCallback extends ViewDragHelper.Callback {
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
mLeft = left;
requestLayout();
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
Log.d("DragLayout", "clampViewPositionHorizontal " + left + "," + dx);
final int leftBound = getPaddingLeft();
final int rightBound = getWidth() - mHandle.getWidth();
final int newLeft = Math.min(Math.max(left, leftBound), rightBound);
return newLeft;
}
@Override
public boolean tryCaptureView(View child, int pointerId) {
return child == mHandle;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//mDragRange = getHeight() - mHandle.getWidth();
/*Log.d("onLayout", "Handle: " + mHandle.getWidth() + ", " + mHandle.getHeight());
Log.d("onLayout", "Drawer: " + mDrawerView.getWidth() + ", " + mDrawerView.getHeight());*/
mHandle.layout(
mLeft,
0,
mLeft + mHandle.getMeasuredWidth(),
b);
mDrawerView.layout(
mLeft + mHandle.getMeasuredWidth(),
0,
mLeft + r,
b);
}
}
mHandle.layout(
mLeft,
0,
mLeft + mHandle.getMeasuredWidth(),
b);
mDrawerView.layout(
mLeft + mHandle.getMeasuredWidth(),
0,
mLeft + r,
b);
}