android聊天界面鍵盤、表情切換絲滑實現(xiàn)的具體思路
1、我們在聊天頁面時候,往往會遇到,鍵盤、表情、其他選擇切換時候頁面會出現(xiàn)掉下來再彈起問題,這是因為,我們切換時候,鍵盤異步導(dǎo)致內(nèi)容View高度變化,頁面掉下來后,又被其他內(nèi)容頂起這種很差視覺效果。
要解決這個問題,最簡單方法就是切換時候,將內(nèi)容View高度固定然后去操作鍵盤顯示后再去釋放內(nèi)容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">
<!-- 標(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 是為了讓鍵盤彈起時候,聊天內(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>
<!-- 圖片選擇、語音、視頻等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:當(dāng)鍵盤需要彈起鎖內(nèi)容View高度(這里重點講解參數(shù):height,height = smartRefreshLayoutMaxHeight(聊天內(nèi)容最大高度) - supportSoftInputHeight(鍵盤的高度),這樣做的目前就是讓鍵盤彈起時候,頁面感覺聊天內(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ù)(切換、鍵盤、表情等)
public void recyclerStopScroll() {
recyclerView.stopScroll();
layoutManager.scrollToPositionWithOffset(0, 0);
}3:切換流程
界面正常展示(此時聊天內(nèi)容界面最大高度展示)--->彈起鍵盤
①、RecyclerView停止所有事件recyclerStopScrol()
②、內(nèi)容View鎖高 viewLockHeight(contentViewMaxHeight)
③、起鍵盤
④、延遲釋放高度viewReleaseLockHeight()
彈起鍵盤——>表情
①、RecyclerView停止所有事件recyclerStopScrol()
②、內(nèi)容View鎖高 viewLockHeight(0)
③、收鍵盤
④、展示表情
⑤、延遲釋放高度viewReleaseLockHeight()
表情——>彈起鍵盤
①、RecyclerView停止所有事件recyclerStopScrol()
②、內(nèi)容View鎖高 viewLockHeight(0)
③、彈起鍵盤
④、收起表情
⑤、延遲釋放高度viewReleaseLockHeight()
總結(jié)
到此這篇關(guān)于android聊天界面鍵盤、表情切換絲滑實現(xiàn)的文章就介紹到這了,更多相關(guān)android聊天界面鍵盤表情切換絲滑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android獲得當(dāng)前view在屏幕中坐標(biāo)的方法
這篇文章主要介紹了android獲得當(dāng)前view在屏幕中坐標(biāo)的方法,涉及Android針對view坐標(biāo)相關(guān)屬性的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
淺談Android Studio 4.1 更新內(nèi)容
這篇文章主要介紹了淺談Android Studio 4.1 更新內(nèi)容,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
完美解決客戶端webview持有的頁面緩存,不會立即釋放的問題
下面小編就為大家?guī)硪黄昝澜鉀Q客戶端webview持有的頁面緩存,不會立即釋放的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
Flutter 利用CustomScrollView實現(xiàn)滑動效果
我們可以使用ListView將幾個GridView組合在一起實現(xiàn)了不同可滑動組件的粘合,但是這里必須要設(shè)置禁止 GridView 的滑動,防止多個滑動組件的沖突。這種方式寫起來不太方便,事實上 Flutter 提供了 CustomScrollView 來粘合多個滑動組件,并且可以實現(xiàn)更有趣的滑動效果。2021-06-06
Android音頻開發(fā)之錄制音頻(WAV及MP3格式)
這篇文章主要為大家介紹了Android如何實現(xiàn)音頻文件的錄制(WAV及MP3格式),文中代碼具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
學(xué)習(xí)使用Android Chronometer計時器
Chronometer是一個簡單的計時器,你可以給它一個開始時間,并以計時,或者如果你不給它一個開始時間,它將會使用你的時間通話開始,這篇文章主要幫助大家學(xué)習(xí)掌握使用Android Chronometer計時器,感興趣的小伙伴們可以參考一下2016-04-04

