首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ViewGroup onLayout问题

ViewGroup onLayout问题
EN

Stack Overflow用户
提问于 2011-07-27 18:53:13
回答 1查看 5.4K关注 0票数 2

我的自定义ViewGroup有问题。我将3个子对象排成一行,并使用滚动条滚动到中间的子对象。根据用户的触摸输入,我更改了应该显示的子项,并请求一个新的布局。然后,我以新的顺序排列子对象。但是,当在以前的布局运行中显示了一个子项时,这些子项会一个接一个地躺在一起。我检查了孩子们以正确的方式获得布局,我认为旧的布局没有被删除,新的孩子只是在旧的布局上绘制。有没有办法确保旧的布局被清除呢?

下面是我的代码:

代码语言:javascript
复制
@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);
    ...
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-20 21:54:57

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6843138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档