我一直在關注這個演練的ViewDragHelperViewDragHelper堆棧溢出
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);
}
請用你的XML佈局更新你的問題。 – Basbous