为什么拖观点与ViewDragHelper恢复到原来的位置上的布局()?布局、观点、位置、ViewDragHelper

2023-09-06 02:08:09 作者:雾里看小花

我实现我的游戏中自定义扩展的操作栏。 我使用的ViewDragHelper处理栏视图,它是包含在拖动急张和一个的LinearLayout 我的子类来连接ViewDragHelper。

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:

教程上的ViewDragHelper:http://flavienlaurent.com/blog/2013/08/28/each-navigation-drawer-hides-a-viewdraghelper/ 在掌握了Android触控系统: https://www.youtube.com/watch?v=EZAoJU-nUyI (这给了我,他们键使浏览点击的和的可拖动) Tutorial on the ViewDragHelper: http://flavienlaurent.com/blog/2013/08/28/each-navigation-drawer-hides-a-viewdraghelper/ Mastering the Android touch system: https://www.youtube.com/watch?v=EZAoJU-nUyI (which has given me they key to make Views clickable and draggable)

我遇到的唯一问题是在布局()我父母的LinearLayout的行为:在每次调用布局() / onLayout(),孩子可拖动/可扩展的操作栏是重置到原来的位置(一个在XML布局集)。

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).

这是为什么?

(以我的经验布局()从来没有食堂用的观点,即已经被移动了位置)

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

推荐答案

我正在使用的解决方法是每次拖动操作后,记录视图的偏移量,并重新它们 onLayout(),例如:

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);
}