Android 自定义View之展开收起的Layout

ok,这个就是重点了。

因为只是view本身高度的变化,我们只需要重写onMeasure去计算高度即可。

来看onMeasure

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec)

//重置高度

allChildHeight = 0

firstChildHeight = 0

if (childCount > 0) {

//遍历计算高度

for (index in 0 until childCount) {

//这个地方实际使用中除了measuredHeight,以及margin等,也要计算在内

if (index == 0) {

firstChildHeight = getChildAt(index).measuredHeight

+getChildAt(index).marginTop + getChildAt(index).marginBottom

+this.paddingTop + this.paddingBottom

}

//实际使用时或包括padding等

allChildHeight += getChildAt(index).measuredHeight + getChildAt(index).marginTop + getChildAt(index).marginBottom

//最后一条的时候 加上当前view自身的padding

if (index == childCount - 1) {

allChildHeight += this.paddingTop + this.paddingBottom

}

}

// 根据是否展开设置高度

if (isOpen) {

setMeasuredDimension(

widthMeasureSpec,

firstChildHeight + ((allChildHeight - firstChildHeight) * animPercent).toInt()

)

} else {

setMeasuredDimension(

widthMeasureSpec,

allChildHeight - ((allChildHeight - firstChildHeight) * animPercent).toInt()

)

}

}

}

onMeasure里面也是分了两个步骤的:

  • 遍历计算高度

//遍历计算高度

for (index in 0 until childCount) {

//这个地方实际使用中除了measuredHeight,以及margin等,也要计算在内

if (index == 0) {

firstChildHeight = getChildAt(index).measuredHeight

+getChildAt(index).marginTop + getChildAt(index).marginBottom

+this.paddingTop + this.paddingBottom

}

//实际使用时或包括padding等

allChildHeight += getChildAt(index).measuredHeight + getChildAt(index).marginTop + getChildAt(index).marginBottom

//最后一条的时候 加上当前view自身的padding

if (index == childCount - 1) {

allChildHeight += this.paddingTop + this.paddingBottom

}

}

来看第一个if判断,记录了第一个子view的高度,这里需要注意,除了measuredHeightmargin也要算上,而且父view的内边距padding也要加上,因为如果父view的padding很大的话,收起时view可能会显示不出来的。

然后就是总高度的计算,道理同上。

来看最后一个if判断,同样总高度计算完之后也要加上父view的上下padding,才是完整的高度。

第一个判断可以理解为收起状态的高度,第二个判断可以理解为展开状态的高度。

  • 展开收起逻辑

// 根据是否展开设置高度

if (isOpen) {

setMeasuredDimension(

widthMeasureSpec,

firstChildHeight + ((allChildHeight - firstChildHeight) * animPercent).toInt()

)

} else {

setMeasuredDimension(

widthMeasureSpec,

allChildHeight - ((allChildHeight - firstChildHeight) * animPercent).toInt()

)

}

因为第一个子view是保留显示的,所以在计算的时候都需要减去第一个子view的高度,就是剩余高度

剩余高度可以很简单的计算出来,但是如何在显示的时候不突兀呢。

这里加一个动画,根据动画的执行进度来计算。

展开:第一个子view的高度 + 剩余高度 × 0到1的Float动画值

收起:总高度 - 剩余高度 × 0到1的Float动画值

author:yechaoa

动画

写一个方法控制展开收起,并在展开收起的时候执行动画。

fun toggle(): Boolean {

isOpen = !isOpen

startAnim()

return isOpen

}

/**

  • 执行动画的时候 更改 animPercent 属性的值 即从0-1

*/

@SuppressLint(“AnimatorKeep”)

private fun startAnim() {

//ofFloat,of xxxX 根据参数类型来确定

//1,动画对象,即当前view。2.动画属性名。3,起始值。4,目标值。

val animator = ObjectAnimator.ofFloat(this, “animPercent”, 0f, 1f)

animator.duration = 500

animator.start()

}

并修改我们的动画参数:

/**

  • 动画值改变的时候 请求重新布局

*/

private var animPercent: Float = 0f

set(value) {

field = value

requestLayout()

}

set value的时候调用requestLayout(),重新执行onMeasure

调用

  • xml

<com.yechaoa.customviews.expand.ExpandLinearLayout

android:id=“@+id/ell”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:background=“#f5f5f5”

android:orientation=“vertical”

android:padding=“10dp”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:padding=“10dp”

android:text=“@string/app_name”

android:textColor=“@android:color/holo_red_dark”

android:textSize=“20sp” />

</com.yechaoa.customviews.expand.ExpandLinearLayout>

  • 代码

ll_btn.setOnClickListener {

val toggle = ell.toggle()

tv_tip.text = if (toggle) “收起” else “展开”

}

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
lbeEOWX-1715365904238)]

[外链图片转存中…(img-AealwYh7-1715365904240)]

[外链图片转存中…(img-FWAkQfNI-1715365904241)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ToggleExpandLayout是一个可折叠和展开view的开关布局控件。它可以将它的子view以阶梯式的展开。项目地址:https://github.com/fenjuly/ToggleExpandLayout 效果图:如何使用<com.fenjuly.mylibrary.ToggleExpandLayout             android:id="@ id/toogleLayout"             android:layout_width="wrap_content"             android:layout_height="80dp"             >             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 1"/>             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 2"/>             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view"/>                      </com.fenjuly.mylibrary.ToggleExpandLayout>注意,由于ToggleExpandLayout的本质是个FrameLayout,所以必须将其高度设置为大于所有子view展开状态的高度,不能设为wrap_content。为了解决这个问题,你可以将ToggleExpandLayout的外面在加个DropDownLayout:<com.fenjuly.mylibrary.DropDownLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         >         <com.fenjuly.mylibrary.ToggleExpandLayout             android:id="@ id/toogleLayout"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             >             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 1"/>              <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 2"/>               <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view"/>                              </com.fenjuly.mylibrary.ToggleExpandLayout> </com.fenjuly.mylibrary.DropDownL

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值