【问题标题】:ViewGroup onLayout ProblemViewGroup onLayout 问题
【发布时间】:2011-07-27 10:53:13
【问题描述】:

我的自定义 ViewGroup 有问题。我连续布局 3 个孩子,并使用 Scroller 滚动到中间孩子。根据用户的触摸输入,我更改应该显示的子项并请求新的布局。然后我以新的顺序排列孩子。但是当一个孩子已经在之前的布局中显示出来时,孩子们就躺在彼此的上面。我检查了孩子们是否以正确的方式获得了布局,我认为旧布局没有被删除,新的孩子只是在旧布局之上绘制。有没有办法确保旧布局被清除或其他什么?

这是我的代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
    ...
    case MotionEvent.ACTION_UP:
        if(old != current)
            this.requestLayout();
        else
            this.scrollTo(getWidth(), 0);
    ...
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // show current-1, current, current+1
    int count = 0;
    for(int i = 1; i >= -1; i--) {
        // determine index of child
        // the mod does a modulo
        int index = mod(current-i, getChildCount());
        // position in row from left to right
        this.getChildAt(index).layout(count*this.getWidth(), 0, (count+1)*this.getWidth(), height);
        count++;
    }
    // scroll to middle view
    this.scrollTo(getWidth(), 0);
    ...
}

【问题讨论】:

    标签: android viewgroup


    【解决方案1】:

    尝试在onLayout()末尾调用invalidate()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多