龟兔赛竞走 2017-01-16 08:36 采纳率: 50%
浏览 4591
已采纳

Android 自定义ViewGroup addView,视图不刷新

     class AutoArrangeLayout(context: Context, attrs: AttributeSet?) : ViewGroup(context, attrs) {
    var horizontalSpacing: Int = 0
        set(value) {
            field = value
            requestLayout()
        }
    var verticalSpacing: Int = 0
        set(value) {
            field = value
            requestLayout()
        }

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.AutoArrangeLayout)
        horizontalSpacing = ta.getDimensionPixelSize(R.styleable.AutoArrangeLayout_horizontalSpacing, 20)
        verticalSpacing = ta.getDimensionPixelSize(R.styleable.AutoArrangeLayout_verticalSpacing, 20)
        ta.recycle()
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        if (changed) {
            (0..childCount)
                    .asSequence()
                    .map { getChildAt(it) }
                    .forEach {
                        it?.let {
                            val get = map[it]
                            if (get != null) {
                                it.layout(get.x, get.y, get.x + it.measuredWidth, get.y + it.measuredHeight)
                            }
                        }
                    }
        }
    }

    var map: HashMap<View, Point> = HashMap()

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var length = paddingLeft
        var rowHeight = 0
        var lastLineHeight = paddingTop + verticalSpacing
        map.clear()
        (0..childCount).
                map { getChildAt(it) }
                .forEach {
                    it?.let {
                        measureChild(it, widthMeasureSpec, heightMeasureSpec)
                        if (it.measuredWidth + length + horizontalSpacing + paddingLeft + paddingRight <= measuredWidth) {
                            val point = Point()
                            length += horizontalSpacing
                            point.x = length
                            point.y = lastLineHeight
                            map.put(it, point)
                            length += it.measuredWidth
                            rowHeight = Math.max(it.measuredHeight, rowHeight)
                        } else {
                            length = horizontalSpacing + paddingLeft
                            lastLineHeight += rowHeight + verticalSpacing
                            val point = Point()
                            point.x = length
                            point.y = lastLineHeight
                            map.put(it, point)
                            length += it.measuredWidth
                            rowHeight = it.measuredHeight
                        }
                    }
                }

        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        var calcHeightMeasureSpec = heightMeasureSpec

        if (heightMode == MeasureSpec.AT_MOST) {
            calcHeightMeasureSpec = MeasureSpec.makeMeasureSpec(lastLineHeight + rowHeight + verticalSpacing, MeasureSpec.EXACTLY)
        }

        super.onMeasure(widthMeasureSpec, calcHeightMeasureSpec)
    }

    }

上述是自定义ViewGroup的类代码,是一个自动排列布局.
测试的时候在界面上还有个btn,点击一次会调用一次该类实例的addView方法,但是发现如果addView后,在该类的高设为wrap的情况下,如果高不足以新换一行则addView添加的视图不显示,若足够新换一行,则会连同之前添加的未显示的view一次性全部显示到界面上。

  • 写回答

3条回答 默认 最新

  • IT小小搬运工 2017-01-16 09:22
    关注

    看看这个有没有帮助 http://blog.csdn.net/zhanglongit/article/details/42459199
    另外可以看看强仔老师的Android自定义View课程,有问题还可以向老师提问http://www.atstudy.com/course/142

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器