ViewDragHelper做拖动,View会跳回到原位置,why?

使用ViewDragHelper做拖动,当动态addView的时候,原本已经拖动到另一位置的View会回到原位置,why?

我有一个简单的VHLayout,继承自FrameLayout,里面通过setDragableView方法可以指定哪个子View可以拖动

 import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class VHLayout extends FrameLayout {

    private View mView;
    private ViewDragHelper mDragHelper;

    public VHLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            // 返回ture则表示可以捕获该view;可以根据传入的view参数决定哪些可以拖动
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return child == mView;
            }

            // 控制水平拖动的边界
            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                final int leftBound = getPaddingLeft();
                final int rightBound = getWidth() - child.getWidth() - leftBound;
                final int newLeft = Math.min(Math.max(left, leftBound), rightBound);
                return newLeft;
            }

            // 控制垂直拖动的边界
            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                final int topBound = getPaddingTop();
                final int bottomBound = getHeight() - child.getHeight() - topBound;
                final int newTop = Math.min(Math.max(top, topBound), bottomBound);
                return newTop;
            }

            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                super.onViewReleased(releasedChild, xvel, yvel);
            }

            // 解决ViewDragHelper中添加button,在控制button拖动的同时又不影响button的点击事件!
            @Override
            public int getViewHorizontalDragRange(View child) {
                return mView == child ? child.getWidth() : 0;
            }

            @Override
            public int getViewVerticalDragRange(View child) {
                return mView == child ? child.getHeight() : 0;
            }

            @Override
            public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
                super.onViewPositionChanged(changedView, left, top, dx, dy);
                changedView.layout(left, top, left + changedView.getMeasuredWidth(),
                        top + changedView.getMeasuredHeight());
            }

        });
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        final int action = MotionEventCompat.getActionMasked(event);
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            mDragHelper.cancel();
            return false;
        }
        return mDragHelper.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        try {
            mDragHelper.processTouchEvent(event);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return true;
    }

    public void setDragableView(View view) {
        mView = view;
    }
}

拖动一切没问题
但是当我实际使用的时候却有问题了,如下,我在布局里的一个叫container的LinearLayout动态添加一个View(addView),就出现所说的现象。事实上,只要弹出输入法,也会出现这种回到原位置的问题。怎么解决的?

使用方式:

 public void addFloatView(int bottomMargin){
        FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        params.gravity=Gravity.RIGHT|Gravity.BOTTOM;//让加入的View出现在右下角
        params.bottomMargin=bottomMargin;

        ImageView iv=new ImageView(context);
        iv.setImageResource(R.drawable.ic_scan);
        iv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                onFloatViewClick();

            }
        });
        VHLayout rootView=(VHLayout)findViewById(R.id.rootView);
        rootView.setDragableView(iv);
        rootView.addView(iv,params);
    }

布局:

 <com.mb.goods.widget.VHLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <include layout="@layout/actionbar_comm" />

    </LinearLayout>

</com.mb.goods.widget.VHLayout>

楼主解决这个问题了吗?我也遇到了一样的问题

刚遇到了相同的问题, 用一个变量标记是否是首次布局, 并用用其它变量保存重新绘制前的状态, 在onlayout 恢复之前的布局状态

估计楼主肯定解决了,留给后来人遇到相同问题的参考.

可以到网上找一下SlidingUpPanelLayout源码看看
https://github.com/umano/AndroidSlidingUpPanel

遇到同样的问题不知道你们咋解决的

这个问题我已经解决了 提供链接希望帮助一样和我出现这个问题的人 https://github.com/dalong982242260/SuspendedBall

我也遇到这个问题了,头疼了很多天啊啊啊啊啊啊啊啊啊啊啊啊

@Override
public void onViewReleased(View child, float xvel, float yvel) {
FrameLayout.LayoutParams st = (FrameLayout.LayoutParams) child.getLayoutParams();
st.gravity = 0;
st.leftMargin = child.getLeft();
st.topMargin = child.getTop();
child.setLayoutParams(st);
}

            保存View 的位置,键盘弹出时就不会发生还原了