android聊天界面鍵盤(pán)、表情切換絲滑實(shí)現(xiàn)的具體思路
1、我們?cè)诹奶祉?yè)面時(shí)候,往往會(huì)遇到,鍵盤(pán)、表情、其他選擇切換時(shí)候頁(yè)面會(huì)出現(xiàn)掉下來(lái)再?gòu)椘饐?wèn)題,這是因?yàn)椋覀兦袚Q時(shí)候,鍵盤(pán)異步導(dǎo)致內(nèi)容View高度變化,頁(yè)面掉下來(lái)后,又被其他內(nèi)容頂起這種很差視覺(jué)效果。
要解決這個(gè)問(wèn)題,最簡(jiǎn)單方法就是切換時(shí)候,將內(nèi)容View高度固定然后去操作鍵盤(pán)顯示后再去釋放內(nèi)容View高度。
2、這里我們提供具體思路
2.1xml布局:(FrameLayout + RecyclerView,是為了讓鍵盤(pán)彈起時(shí)候,RecyclerView有個(gè)向上平移效果)
<?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"> <!-- 標(biāo)題View --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="?actionBarSize"> </androidx.constraintlayout.widget.ConstraintLayout> <!-- 聊天展示View android:layout_weight="1" 讓聊天內(nèi)容填充剩下內(nèi)容--> <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 是為了讓鍵盤(pán)彈起時(shí)候,聊天內(nèi)容(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> <!-- 按鈕:發(fā)送、輸入框等View --> <LinearLayout android:id="@+id/button_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout> <!-- 圖片選擇、語(yǔ)音、視頻等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 這個(gè)是自定義View--> <EmotionView android:id="@+id/emotion" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" /> </LinearLayout>
2.2:當(dāng)鍵盤(pán)需要彈起鎖內(nèi)容View高度(這里重點(diǎn)講解參數(shù):height,height = smartRefreshLayoutMaxHeight(聊天內(nèi)容最大高度) - supportSoftInputHeight(鍵盤(pán)的高度),這樣做的目前就是讓鍵盤(pán)彈起時(shí)候,頁(yè)面感覺(jué)聊天內(nèi)容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:延遲釋放高度(設(shè)置 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展示最后一條數(shù)據(jù)(切換、鍵盤(pán)、表情等)
public void recyclerStopScroll() { recyclerView.stopScroll(); layoutManager.scrollToPositionWithOffset(0, 0); }
3:切換流程
界面正常展示(此時(shí)聊天內(nèi)容界面最大高度展示)--->彈起鍵盤(pán)
①、RecyclerView停止所有事件recyclerStopScrol()
②、內(nèi)容View鎖高 viewLockHeight(contentViewMaxHeight)
③、起鍵盤(pán)
④、延遲釋放高度viewReleaseLockHeight()
彈起鍵盤(pán)——>表情
①、RecyclerView停止所有事件recyclerStopScrol()
②、內(nèi)容View鎖高 viewLockHeight(0)
③、收鍵盤(pán)
④、展示表情
⑤、延遲釋放高度viewReleaseLockHeight()
表情——>彈起鍵盤(pán)
①、RecyclerView停止所有事件recyclerStopScrol()
②、內(nèi)容View鎖高 viewLockHeight(0)
③、彈起鍵盤(pán)
④、收起表情
⑤、延遲釋放高度viewReleaseLockHeight()
總結(jié)
到此這篇關(guān)于android聊天界面鍵盤(pán)、表情切換絲滑實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)android聊天界面鍵盤(pán)表情切換絲滑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android用注解與反射實(shí)現(xiàn)Butterknife功能
Butterknife是一個(gè)在android上實(shí)現(xiàn)ioc(控制反轉(zhuǎn))的一個(gè)庫(kù)。ioc的核心是解耦。解耦的目的是修改耦合對(duì)象時(shí)不影響另外一個(gè)對(duì)象,降低模塊之間的關(guān)聯(lián)。在Spring中ioc更多的是依靠xml的配置。而android上的IOC框架可以不使用xml配置2022-11-11Android三種方式生成矢量圖之VectorDrawable類使用詳解
這篇文章主要介紹了Android三種方式生成矢量圖的VectorDrawable類,2014年6月26日的I/O?2014開(kāi)發(fā)者大會(huì)上谷歌正式推出了Android?L,它帶來(lái)了全新的設(shè)計(jì)語(yǔ)言Material?Design,新的API也提供了這個(gè)類VectorDrawable2023-02-02Android RecyclerView多類型布局卡片解決方案
這篇文章主要介紹了Android RecyclerView多類型布局卡片解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03Android?IdleHandler基本使用及應(yīng)用案例詳解
這篇文章主要為大家詳細(xì)介紹了Android?IdleHandler的基本使用及應(yīng)用案例,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以參考一下2022-10-10Android自定義定時(shí)鬧鐘開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了Android自定義定時(shí)鬧鐘開(kāi)發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android package屬性、package name和Application ID三者的聯(lián)系及區(qū)別
這篇文章主要介紹了Android package屬性、package name和Application ID三者的聯(lián)系及區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-12-12android仿新聞閱讀器菜單彈出效果實(shí)例(附源碼DEMO下載)
本篇文章介紹了android仿新聞閱讀器菜單彈出效果實(shí)例,現(xiàn)在很多閱讀器都有這個(gè)功能,需要的朋友可以看一下。2016-11-11Android評(píng)分RationBar控件使用詳解
這篇文章主要為大家詳細(xì)介紹了Android評(píng)分RationBar控件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12簡(jiǎn)單了解Android性能優(yōu)化方向及相關(guān)工具
這篇文章主要介紹了簡(jiǎn)單了解Android性能優(yōu)化方向及相關(guān)工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08