2

我的自定义 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);
    ...
}
4

1 回答 1

1

尝试invalidate()在结束时调用onLayout()

于 2011-12-20T13:54:57.073 回答