gpt4 book ai didi

android - ViewGroup onLayout问题

转载 作者:行者123 更新时间:2023-11-29 02:11:46 26 4
gpt4 key购买 nike

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

这是我的代码:

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

最佳答案

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

关于android - ViewGroup onLayout问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6843138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com