44人参与 • 2025-02-13 • Android
1、我们在聊天页面时候,往往会遇到,键盘、表情、其他选择切换时候页面会出现掉下来再弹起问题,这是因为,我们切换时候,键盘异步导致内容view高度变化,页面掉下来后,又被其他内容顶起这种很差视觉效果。
要解决这个问题,最简单方法就是切换时候,将内容view高度固定然后去操作键盘显示后再去释放内容view高度。
2、这里我们提供具体思路
2.1xml布局:(framelayout + recyclerview,是为了让键盘弹起时候,recyclerview有个向上平移效果)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 标题view --> <androidx.constraintlayout.widget.constraintlayout android:layout_width="match_parent" android:layout_height="?actionbarsize"> </androidx.constraintlayout.widget.constraintlayout> <!-- 聊天展示view android:layout_weight="1" 让聊天内容填充剩下内容--> <com.scwang.smart.refresh.layout.smartrefreshlayout android:id="@+id/smartrefreshlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" app:srlenableloadmore="false" app:srlenablerefresh="true"> <!-- 添加framelayout 是为了让键盘弹起时候,聊天内容(recyclerview)平移上去效果--> <framelayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.recyclerview android:id="@+id/recyler" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="bottom" android:overscrollmode="never" android:scrollbars="none" android:visibility="invisible" /> </framelayout> </com.scwang.smart.refresh.layout.smartrefreshlayout> <!-- 按钮:发送、输入框等view --> <linearlayout android:id="@+id/button_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> </linearlayout> <!-- 图片选择、语音、视频等view --> <androidx.constraintlayout.widget.constraintlayout android:id="@+id/other_select" android:layout_width="match_parent" android:layout_height="@dimen/common_dp_114" android:visibility="gone"> </androidx.constraintlayout.widget.constraintlayout> <!-- emotion 表情选择view 这个是自定义view--> <emotionview android:id="@+id/emotion" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" /> </linearlayout>
2.2:当键盘需要弹起锁内容view高度(这里重点讲解参数:height,height = smartrefreshlayoutmaxheight(聊天内容最大高度) - supportsoftinputheight(键盘的高度),这样做的目前就是让键盘弹起时候,页面感觉聊天内容view平移上效果)
private void viewlockheight(int height) { linearlayout.layoutparams layoutparams = (linearlayout.layoutparams) smartrefreshlayout.getlayoutparams(); layoutparams.height = height == 0 ? smartrefreshlayout.getheight() : height; layoutparams.weight = 0.0f; smartrefreshlayout.setlayoutparams(layoutparams); }
2.3:延迟释放高度(设置 layoutparams.weight = 1.0f)
private void viewreleaselockheight(int delaymillis) { if (smartrefreshlayout != null) { smartrefreshlayout.postdelayed(new runnable() { @override public void run() { if (smartrefreshlayout != null) { linearlayout.layoutparams layoutparams = (linearlayout.layoutparams) smartrefreshlayout.getlayoutparams(); layoutparams.height = linearlayout.layoutparams.match_parent; layoutparams.weight = 1.0f; smartrefreshlayout.setlayoutparams(layoutparams); } } }, delaymillis == 0 ? 200l : delaymillis); } }
2.4:recyclerview展示最后一条数据(切换、键盘、表情等)
public void recyclerstopscroll() { recyclerview.stopscroll(); layoutmanager.scrolltopositionwithoffset(0, 0); }
3:切换流程
界面正常展示(此时聊天内容界面最大高度展示)--->弹起键盘
①、recyclerview停止所有事件recyclerstopscrol()
②、内容view锁高 viewlockheight(contentviewmaxheight)
③、起键盘
④、延迟释放高度viewreleaselockheight()
弹起键盘——>表情
①、recyclerview停止所有事件recyclerstopscrol()
②、内容view锁高 viewlockheight(0)
③、收键盘
④、展示表情
⑤、延迟释放高度viewreleaselockheight()
表情——>弹起键盘
①、recyclerview停止所有事件recyclerstopscrol()
②、内容view锁高 viewlockheight(0)
③、弹起键盘
④、收起表情
⑤、延迟释放高度viewreleaselockheight()
总结
到此这篇关于android聊天界面键盘、表情切换丝滑实现的文章就介绍到这了,更多相关android聊天界面键盘表情切换丝滑内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论