(原创)可支持最大高度的NestedScrollView

前言

最近写一个dialog,里面有一个ScrollView需要设置最大的高度
但是发现ScrollView是不支持最大高度的
于是自己动手写一个出来

代码

代码很简单,直接贴出来

class MaxHeightNestedScrollView : NestedScrollView {
   
   
  private var maxHeightPercent = 0.5f
  constructor(context: Context) : this(context, null)
  constructor(context: Context, attributeSet: AttributeSet?) : this(context
Android开发中,`NestedScrollView` 是一个常用的滚动视图组件,有时我们可能需要设置最大高度以适应特定的布局需求。以下是一些设置 `NestedScrollView` 最大高度的方法: ### 方法一:使用 `wrap_content` 和 `maxHeight` 属性 你可以在XML布局文件中使用 `wrap_content` 来让 `NestedScrollView` 根据内容自适应高度,并通过自定义属性来限制其最大高度。 ```xml <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:maxHeight="200dp"> <!-- 你的内容 --> </androidx.core.widget.NestedScrollView> ``` ### 方法二:使用 `ConstraintLayout` 的约束 你也可以使用 `ConstraintLayout` 来设置 `NestedScrollView` 的最大高度。 ```xml <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.core.widget.NestedScrollView android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHeight_max="200dp"> <!-- 你的内容 --> </androidx.core.widget.NestedScrollView> </androidx.constraintlayout.widget.ConstraintLayout> ``` ### 方法三:在代码中动态设置高度 你也可以在代码中动态设置 `NestedScrollView` 的最大高度。 ```java NestedScrollView nestedScrollView = findViewById(R.id.nestedScrollView); ViewGroup.LayoutParams layoutParams = nestedScrollView.getLayoutParams(); layoutParams.height = dpToPx(200); nestedScrollView.setLayoutParams(layoutParams); private int dpToPx(int dp) { float density = getResources().getDisplayMetrics().density; return Math.round((float) dp * density); } ``` ### 方法四:使用 `android:maxHeight` 属性(需要自定义视图) 如果你需要更复杂的逻辑,可以创建一个自定义视图来限制 `NestedScrollView` 的最大高度。 ```java public class MaxHeightNestedScrollView extends NestedScrollView { private int maxHeight; public MaxHeightNestedScrollView(Context context) { super(context); } public MaxHeightNestedScrollView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightNestedScrollView); maxHeight = a.getDimensionPixelSize(R.styleable.MaxHeightNestedScrollView_maxHeight, -1); a.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (maxHeight != -1) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } ``` 在XML中使用自定义视图: ```xml <com.example.MaxHeightNestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content" app:maxHeight="200dp"> <!-- 你的内容 --> </com.example.MaxHeightNestedScrollView> ``` 通过以上方法,你可以根据具体需求设置 `NestedScrollView` 的最大高度
下载前可以先看下教程 https://pan.quark.cn/s/7e1151b22ec2 在Android开发领域,NestedScrollView是谷歌为应对滑动组件嵌套场景而设计的一种视图容器,该容器能够在一个具备滚动能力的视图内部嵌入另一个可滚动的视图,例如RecycleView。 本篇文章将详细阐述NestedScrollView的应用方式,以及如何借助此容器优化UI性能,从而规避在ScrollView中嵌套ListView时产生的卡顿现象。 ### NestedScrollView概述NestedScrollView属于Android Support Library中的一个类别,它不仅继承了ScrollView的特性,还增加了对Android的Nested Scroll协议的支持。 这意味着NestedScrollView能够与遵循此协议的其他视图(例如RecyclerView)进行协同滚动,进而为用户带来更加顺畅的操作感受。 ### 解决ScrollView嵌套ListView的卡顿问题当ScrollView内部包含ListView时,由于两个视图均试图管理触摸事件与滚动行为,这会导致性能的下降,特别是在内容量较大时,可能会出现较为明显的卡顿现象。 NestedScrollView针对这一问题提供了有效的解决方案,它允许在一个可滚动的视图内部嵌入另一个可滚动的视图,并且能够高效地处理滚动事件,确保用户获得流畅的滚动体验。 ### 使用NestedScrollView要应用NestedScrollView,首先需要在build.gradle文件中引入Android Support Library:```groovydependencies { implementati...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值