Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

android - Why is view dragged with ViewDragHelper reset to its original position on layout()?

I am implementing a custom expandable action bar in my game. I am using ViewDragHelper to handle the dragging and flinging of the bar view, which is contained in a LinearLayout I subclassed to attach the ViewDragHelper.

The following links have been of great help to achieve this:

The only issue I am encountering is the behavior of the layout() of my parent LinearLayout: on every call to layout() / onLayout(), the child draggable/expandable action bar is reset to its original position (the one set in the XML layout).

Why is that ?

(In my experience layout() never messes with the positions of views that have already been moved)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The workaround I'm using is to record the view's offsets after each drag operation, and reapply them in onLayout(), e.g.

View mVdhView;
int mVdhXOffset;
int mVdhYOffset;

@Override
public void computeScroll() {
    if (dragHelper.continueSettling(true)) {
        postInvalidateOnAnimation();
    } else {
        mVdhXOffset = mVdhView.getLeft();
        mVdhYOffset = mVdhView.getTop();
    }
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);

      // Reapply VDH offsets
    mVdhView.offsetLeftAndRight(mVdhXOffset);
    mVdhView.offsetTopAndBottom(mVdhYOffset);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...