我一直在关注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);
}
答案 0 :(得分:-1)
根据堆栈跟踪,您对DragLayout的onLayout进行了递归调用。
你的DragLayout只调用两个视图的布局(...):mHandle& mDrawerView似乎其中一个(无法通过日志判断哪一个)导致堆栈溢出。
请发布您的观看代码,以便我可以帮您找到问题。