Java ViewDragHelper堆栈溢出
我一直在关注ViewDragHelper的此演练 我目前正在尝试实现自定义布局,但ViewDragHelper有问题。我试图得到一个“抽屉”式布局,带有从右到左(即水平)打开的拖动手柄。我知道SliderDrawer可以处理这个任务,但我的目标只是学习ViewDrawer。这是我得到的错误:Java ViewDragHelper堆栈溢出,java,android,android-layout,android-view,Java,Android,Android Layout,Android View,我一直在关注ViewDragHelper的此演练 我目前正在尝试实现自定义布局,但ViewDragHelper有问题。我试图得到一个“抽屉”式布局,带有从右到左(即水平)打开的拖动手柄。我知道SliderDrawer可以处理这个任务,但我的目标只是学习ViewDrawer。这是我得到的错误: 09-24 14:27:21.031: E/AndroidRuntime(24002): FATAL EXCEPTION: main 09-24 14:27:21.031: E/AndroidRuntim
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);
}
根据堆栈跟踪,可以递归调用onLayout-of-DragLayout 您的DragLayout仅为两个视图调用layout(…):mHandle和mDrawerView,并且其中一个视图(无法通过日志判断是哪一个视图)导致堆栈溢出
请发布您的视图代码,以便我可以尝试帮助您找到问题。请使用XML布局更新您的问题。这不是答案。请在评论中发布您想要的澄清