我的自定义ViewGroup有问题。我将3个子对象排成一行,并使用滚动条滚动到中间的子对象。根据用户的触摸输入,我更改了应该显示的子项,并请求一个新的布局。然后,我以新的顺序排列子对象。但是,当在以前的布局运行中显示了一个子项时,这些子项会一个接一个地躺在一起。我检查了孩子们以正确的方式获得布局,我认为旧的布局没有被删除,新的孩子只是在旧的布局上绘制。有没有办法确保旧的布局被清除呢?
下面是我的代码:
@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);
...
}发布于 2011-12-20 21:54:57
尝试在onLayout()结束时调用invalidate()。
https://stackoverflow.com/questions/6843138
复制相似问题