ViewGroup中onLayout问题 [英] ViewGroup onLayout Problem

查看:320
本文介绍了ViewGroup中onLayout问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的自定义ViewGroup中的一个问题。我布局的孩子3成一排,并使用滚轮滚动到中间的孩子。基于用户的触摸输入变更应显示和请求新的布局中的儿童。然后,我布局孩子们在新的秩序。但是,当一个孩子已经显示在一个previous布局运行儿童躺在彼此的顶部。我检查了孩子们以正确的方式布局,我觉得旧的布局不会被删除,新的孩子们得到的只是画在旧的布局上。有没有一种方法,以确保老的布局被清除的东西?

下面是我的code:

  @覆盖
公共布尔onTouchEvent(MotionEvent事件){
    ...
    案例MotionEvent.ACTION_UP:
        如果(老!=电流)
            this.requestLayout();
        其他
            this.scrollTo(的getWidth(),0);
    ...
}@覆盖
保护无效onLayout(布尔变化,诠释L,INT T,INT R,INT B){
    //显示当前-1,电流,电流+ 1
    诠释计数= 0;
    的for(int i = 1; I> = -1;我 - ){
        //确定孩子的指数
        //mod做了模
        INT指数= MOD(电流I,getChildCount());
        //在行位置从左至右
        this.getChildAt(指数).layout(计数* this.getWidth(),0,(计数+ 1)* this.getWidth(),高度);
        算上++;
    }
    //滚动到中观
    this.scrollTo(的getWidth(),0);
    ...
}


解决方案

尝试调用无效() AT) onLayout结束时(

I have a problem with my custom ViewGroup. I layout 3 of the children in a row and use a Scroller to scroll to the middle child. Based on the touch input of the user I change the children that should be displayed and request a new layout. Then I layout the children in a new order. But when one child has been displayed in a previous layout run the children lie on top of each other. I checked that the children get layout in the right way and I think the old layout is not deleted and the new children get just drawn on top of the old layout. Is there a way to ensure that the old layout gets cleared or something?

Here is my code:

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

解决方案

Try to call invalidate() at the end of onLayout().

这篇关于ViewGroup中onLayout问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆