欢迎来到代码驿站!

当前位置:首页 >

Android自定义ViewGroup多行多列效果

时间:2023-03-01 15:10:40|栏目:|点击:

本文实例为大家分享了Android自定义ViewGroup多行多列的具体代码,供大家参考,具体内容如下

先看下效果图

每行两个子孩子

每行一个子孩子

实现思路

自定义viewGroup,实现测量和布局,使控件适应业务场景。

测量

根据父控件的宽度,平均分给每个子孩子固定的宽度。高度就是行数乘以一个子孩子的高度,再加上空隙的高度。

根据子孩子个数计算行数  

val rows = if (childCount % perLineChild == 0) {
            childCount / perLineChild
        } else {
            childCount / perLineChild + 1
        }

代码示例

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = MeasureSpec.getSize(widthMeasureSpec)
        for (i in 0 until childCount) {
            val child: View = getChildAt(i)
            if (child.visibility != GONE) {
                val lp = child.layoutParams
                val childWidthMeasureSpec = getChildMeasureSpec(
                    widthMeasureSpec,
                    0, (width - (perLineChild - 1) * space) / perLineChild
                )
                val childHeightMeasureSpec = getChildMeasureSpec(
                    heightMeasureSpec,
                    0, lp.height
                )
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec)
            }
        }
        val rows = if (childCount % perLineChild == 0) {
            childCount / perLineChild
        } else {
            childCount / perLineChild + 1
        }
        if (childCount > 0) {
            setMeasuredDimension(
                width,
                getChildAt(0).measuredHeight * rows + (rows - 1) * space
            )
        }
    }

布局

需要注意摆放的顺序和位置,每行摆放固定的个数,达到个数之后换行继续摆放

代码示例 

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        var left = l
        var top = t
        children.forEachIndexed { index, view ->
            if (index % perLineChild == 0) {
                left = 0
                if (index != 0) {
                    top += view.measuredHeight
                    top+=space
                }
                view.layout(left, top, view.measuredWidth + left, top + view.measuredHeight)
            } else {
                view.layout(left, top, view.measuredWidth + left, top + view.measuredHeight)
            }
            left += view.measuredWidth
            left += space
        }
    }

完整代码

class MultiLineViewG @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ViewGroup(context, attrs, defStyleAttr) {

    var perLineChild = 2

    /**
     * 子孩子之间的空隙
     */
    var space = 10

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = MeasureSpec.getSize(widthMeasureSpec)
        for (i in 0 until childCount) {
            val child: View = getChildAt(i)
            if (child.visibility != GONE) {
                val lp = child.layoutParams
                val childWidthMeasureSpec = getChildMeasureSpec(
                    widthMeasureSpec,
                    0, (width - (perLineChild - 1) * space) / perLineChild
                )
                val childHeightMeasureSpec = getChildMeasureSpec(
                    heightMeasureSpec,
                    0, lp.height
                )
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec)
            }
        }
        val rows = if (childCount % perLineChild == 0) {
            childCount / perLineChild
        } else {
            childCount / perLineChild + 1
        }
        if (childCount > 0) {
            setMeasuredDimension(
                width,
                getChildAt(0).measuredHeight * rows + (rows - 1) * space
            )
        }
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        var left = l
        var top = t
        children.forEachIndexed { index, view ->
            if (index % perLineChild == 0) {
                left = 0
                if (index != 0) {
                    top += view.measuredHeight
                    top+=space
                }
                view.layout(left, top, view.measuredWidth + left, top + view.measuredHeight)
            } else {
                view.layout(left, top, view.measuredWidth + left, top + view.measuredHeight)
            }
            left += view.measuredWidth
            left += space
        }
    }
}

上一篇:unity置灰处理的实现

栏    目:

下一篇:android Retrofit2+okHttp3使用总结

本文标题:Android自定义ViewGroup多行多列效果

本文地址:http://www.codeinn.net/misctech/226645.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有