短视频平台源码,自定义流式布局--kotlin

短视频平台源码,自定义流式布局--kotlin实现的相关代码

一、使用原因

1.流式布局中放了许多小的view,要求我们把这些子view妥善的摆放在一个viewgroup中,如果我们在xmL中去实现这个效果,这就要求我们去对每个子view设置margin,padding,还有位置属性。这可能需要花很多时间去摆放,去设置。

2.如果我们做一个搜索内容的历史记录,那么我们事先是不知道子view的条目和具体内容的,所以我们也没法在XML中去书写,那么我们就需要一个viewgroup去对数据进行操作,取自动生成子view,并把他们的位置摆放妥当。

二、使用步骤

1.创建一个类继承viewgroup

代码如下:

 

1
class TagLayout(context: Context?, attrs: AttributeSet?) : ViewGroup(context, attrs)

2.重写onMeasure方法

 

1
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {<br>        //摆放时所有子view占据的横向边界<br>        var widthUsed = 0<br>        //所有子view占据的纵向高度<br>        var heightUsed = 0<br>        //当前行的子view占据的宽度<br>        var lineWidthUsed = 0<br>        //当前行占据的纵向高度<br>        var lineMaxHeight = 0<br>        //拿到布局的期望宽度<br>        val widthMeasureSpecSize = MeasureSpec.getSize(widthMeasureSpec)<br>        for ((index, child) in children.withIndex()) {<br>            //给child进行测量<br>            measureChildWithMargins(<br>                child,<br>                widthMeasureSpec,<br>                0,<br>                heightMeasureSpec,<br>                heightUsed<br>            )<br>            //超出边界时换行<br>            if (child.measuredWidth + lineWidthUsed > widthMeasureSpecSize) {<br>                //累加这行的高度<br>                heightUsed += lineMaxHeight<br>                //新开一行,还没有子view,下面两个值置为零<br>                lineWidthUsed = 0<br>                lineMaxHeight = 0<br>                //由于新开了一行,则对子view进行重新测量<br>                measureChildWithMargins(<br>                    child,<br>                    widthMeasureSpec,<br>                    lineWidthUsed,<br>                    heightMeasureSpec,<br>                    heightUsed<br>                )<br>            }<br>            //如果存放子view对应位置的childrenBounds中还没有存放过这个子view的Rect,则进行添加,避免在onMeasure中对对象进行重复创建。<br>            if (index >= childrenBounds.size) {<br>                childrenBounds.add(<br>                    Rect()<br>                )<br>            }<br>            //设置child 的位置和大小<br>            childrenBounds[index].set(<br>                lineWidthUsed,<br>                heightUsed,<br>                lineWidthUsed + child.measuredWidth,<br>                heightUsed + child.measuredHeight<br>            )<br>            //把新加的子view的宽度进行累加<br>            lineWidthUsed += child.measuredWidth<br>            //更新已加入的所有子view占据的宽度<br>            widthUsed = max(widthUsed, lineWidthUsed)<br>            //更新当前行的view中最高的view高度,既这行子view所占据的高度<br>            lineMaxHeight = max(lineMaxHeight, child.measuredHeight)<br>        }<br>        //获取到所有子view占据的高度,因为最后一行的view高度没有在循环中进行累加,所有循环结束要记得加上<br>        val selfHeight = heightUsed + lineMaxHeight<br>        //获取所有子view占据的宽度<br>        val selfWidth = widthUsed<br>        setMeasuredDimension(selfWidth, selfHeight)<br>    }

 

小结

1.在onMeasure中测量每个子view的大小和位置,并用一个Rect的List用来记录,以供后面onlayout中使用,并且在测量完所有子view后得到viewgroup最合适的大小

2.该处使用0作为第三个参数widthUsed,这是为什么呢?

 

1
 measureChildWithMargins(<br>                child,<br>                widthMeasureSpec,<br>                0,<br>                heightMeasureSpec,<br>                heightUsed<br>            )

 

以上就是 短视频平台源码,自定义流式布局--kotlin实现的相关代码,更多内容欢迎关注之后的文章

 

posted @   云豹科技-苏凌霄  阅读(116)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示