我正在我的游戏中实现一个自定义的可扩展的动作条。
我使用ViewDragHelper来处理条视图的拖放,它包含在附加viewdraghelper的子类LinearLayout中。
以下链接对实现这一目标有很大帮助:
ViewDraghelper教程:http://flavienlaurent.com/blog/2013/08/28/each-navigation-drawer-hides-a-viewdraghelper/
掌握android触摸系统:https://www.youtube.com/watch?v=EZAoJU-nUyI(这给了我使视图可点击和可拖动的关键)
我遇到的唯一问题是父linearlayout的layout()行为:每次调用layout()/onLayout()时,子可拖动/可扩展操作栏都会重置到其原始位置(xml布局中的一个设置)。
为什么?
(以我的经验来看,layout()永远不要搅乱已经移动的视图的位置)

最佳答案

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

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

09-06 05:52