Android Studio聊天页透明背景EditText上方放置RecyclerView的滚动适配问题
解决方案
你只需要给RecyclerView添加两个属性即可实现需求:既保留消息上滑时可在输入框后方显示的效果,又能避免底部消息被输入框遮挡。
方案1:固定高度输入栏(推荐,实现最简单)
直接修改你的RecyclerView布局代码,新增paddingBottom和clipToPadding属性:
<androidx.recyclerview.widget.RecyclerView android:layout_below="@+id/bar_layout" android:id="@+id/recycle_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutManager="LinearLayoutManager" android:stackFromBottom="true" android:transcriptMode="alwaysScroll" android:overScrollMode="never" <!-- 新增属性 start --> android:paddingBottom="70dp" android:clipToPadding="false" <!-- 新增属性 end --> />
说明:
paddingBottom的数值需要和你底部输入栏的总高度(包含输入栏的margin、padding和内部控件高度)保持一致,你可以根据自己的布局调整数值。
方案2:动态高度输入栏(适配输入框行数变化的场景)
如果你的输入框会随着输入内容变高,可通过代码动态监听输入栏高度,实时更新RecyclerView的底部padding:
// Kotlin 示例代码 val bottomInputLayout = findViewById<RelativeLayout>(R.id.bottom) val recyclerView = findViewById<RecyclerView>(R.id.recycle_view) // 监听布局绘制完成,获取输入栏真实高度 bottomInputLayout.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { val layoutParams = bottomInputLayout.layoutParams as MarginLayoutParams // 计算输入栏总高度:自身高度 + 上下margin val totalInputHeight = bottomInputLayout.height + layoutParams.topMargin + layoutParams.bottomMargin // 更新RecyclerView底部padding recyclerView.setPadding( recyclerView.paddingLeft, recyclerView.paddingTop, recyclerView.paddingRight, totalInputHeight ) // 移除监听避免重复触发 bottomInputLayout.viewTreeObserver.removeOnGlobalLayoutListener(this) } })
内容的提问来源于stack exchange,提问作者SSS
相关产品推荐
相关产品推荐

