如何实现Android中ScrollView的上下拉动调整大小功能?
解决方案:让ScrollView支持上下拖拽调整尺寸
方案一:使用BottomSheetBehavior(系统自带,推荐)
这个方案利用Material Components库的BottomSheetBehavior,自带拖拽交互和边界限制,无需自己编写触摸逻辑。
步骤1:确认依赖
确保你的app/build.gradle(Module级别)中包含Material Components依赖:
dependencies { implementation 'com.google.android.material:material:1.9.0' // 版本可根据需求调整 }
步骤2:修改布局代码
将根布局替换为CoordinatorLayout,并给目标ScrollView配置BottomSheetBehavior:
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"/> <!-- 可拖拽调整的ScrollView --> <ScrollView android:id="@+id/to_Resize" android:layout_width="match_parent" android:layout_height="250dp" app:layout_behavior="@string/bottom_sheet_behavior" app:behavior_peekHeight="150dp" <!-- 拖拽后最小高度 --> app:behavior_maxHeight="400dp" <!-- 拖拽后最大高度 --> app:behavior_hideable="false"> <!-- 是否允许完全隐藏,这里设为不允许 --> <LinearLayout android:id="@+id/text_block" android:layout_width="match_parent" android:layout_height="match_parent" android:isScrollContainer="true" android:orientation="vertical" /> </ScrollView> <TextView android:id="@+id/PathShow" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" android:layout_gravity="bottom" /> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF" android:layout_gravity="bottom"> <GridLayout android:id="@+id/pew1" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="4" android:rowCount="12"/> </ScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout>
说明
behavior_peekHeight:设置ScrollView的最小可显示高度,即拖拽到最低位置时的高度behavior_maxHeight:设置ScrollView的最大可拉伸高度behavior_hideable:设为true时允许将ScrollView完全拖出屏幕,可根据需求选择
方案二:自定义拖拽逻辑(手动实现)
如果不想依赖Material库,可以手动添加拖拽手柄并监听触摸事件来调整高度。
步骤1:修改布局,添加拖拽手柄
在目标ScrollView顶部或底部添加一个用于拖拽的View:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"/> <!-- 包裹ScrollView和拖拽手柄 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 拖拽手柄 --> <View android:id="@+id/drag_handle" android:layout_width="match_parent" android:layout_height="30dp" android:background="#CCCCCC" android:clickable="true"/> <ScrollView android:id="@+id/to_Resize" android:layout_width="match_parent" android:layout_height="250dp"> <LinearLayout android:id="@+id/text_block" android:layout_width="match_parent" android:layout_height="match_parent" android:isScrollContainer="true" android:orientation="vertical" /> </ScrollView> </LinearLayout> <TextView android:id="@+id/PathShow" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF"> <GridLayout android:id="@+id/pew1" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="4" android:rowCount="12"/> </ScrollView> </LinearLayout>
步骤2:添加触摸监听代码(以Kotlin为例)
在MainActivity中给拖拽手柄添加触摸事件,动态调整ScrollView的高度:
import android.os.Bundle import android.view.MotionEvent import android.view.View import androidx.appcompat.app.AppCompatActivity import kotlin.math.abs class MainActivity : AppCompatActivity() { private var lastY = 0f private val minHeight = 150 // 最小高度(dp) private val maxHeight = 400 // 最大高度(dp) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val dragHandle = findViewById<View>(R.id.drag_handle) val toResizeScrollView = findViewById<View>(R.id.to_Resize) // dp转px val dpToPx = { dp: Int -> (dp * resources.displayMetrics.density).toInt() } val minHeightPx = dpToPx(minHeight) val maxHeightPx = dpToPx(maxHeight) dragHandle.setOnTouchListener { _, event -> when (event.action) { MotionEvent.ACTION_DOWN -> { lastY = event.rawY true } MotionEvent.ACTION_MOVE -> { val deltaY = lastY - event.rawY lastY = event.rawY // 计算新高度 val currentHeight = toResizeScrollView.layoutParams.height val newHeight = currentHeight + deltaY.toInt() // 限制高度在min和max之间 val finalHeight = newHeight.coerceIn(minHeightPx, maxHeightPx) toResizeScrollView.layoutParams.height = finalHeight toResizeScrollView.requestLayout() true } else -> false } } } }
说明
- 代码通过监听拖拽手柄的触摸事件,计算拖拽距离来调整ScrollView的高度
minHeight和maxHeight可根据需求修改,注意需转换为像素值- Java版本逻辑一致,仅语法不同
内容的提问来源于stack exchange,提问作者PewPew01
相关产品推荐
相关产品推荐

