布局
<kpswitch.widget.KPSwitchRootRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_6_0">
……
<kpswitch.widget.KPSwitchPanelLinearLayout
android:id="@+id/root_panel"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/chat_editAndexpression_layout"
android:background="@color/color_white_normal"
android:orientation="vertical"
android:visibility="gone">
<!-- 面板內(nèi)容 -->
……
</kpswitch.widget.KPSwitchPanelLinearLayout>
……
</kpswitch.widget.KPSwitchRootRelativeLayout>
KeyboardUtil.java
// 用來監(jiān)控鍵盤狀態(tài)兼都,是否顯示以及鍵盤高度等
KeyboardUtil.attach(final Activity activity, IPanelHeightTarget target,/** Nullable **/OnKeyboardShowingListener listener)
// 協(xié)助處理一些面板與鍵盤相關(guān)的事件,對(duì)一些相關(guān)時(shí)間進(jìn)行注冊(cè),切換面板鍵盤等
KPSwitchConflictUtil.attach(final View panelLayout,final View focusView,/** Nullable **/ final SwitchClickListener switchClickListener,SubPanelAndTrigger... subPanelAndTriggers)
在KeyBoardUtil#KeyboardStatusListener中
// 用于計(jì)算鍵盤高度
calculateKeyboardHeight(final int displayHeight)
// 鍵盤是否顯示
calculateKeyboardShowing(final int displayHeight)
處理抖動(dòng)問題
panel切換到keyboard
需要對(duì)panel進(jìn)行隱藏蒙挑,keyboard進(jìn)行顯示(顯示有個(gè)過程存在抖動(dòng)現(xiàn)象),點(diǎn)擊編輯框愚臀,keyboard出現(xiàn)進(jìn)行onMeasure重新布局忆蚀。
KPSwitchRootLinearLayout.java
在handleBeforeMeasure中鍵盤的彈出調(diào)用
panel.handleHide();
/**
* @see #processOnMeasure(int, int)
*/
@Override
public void handleHide() {
this.mIsHide = true; // 此時(shí),panel visible,panel被重新繪制
}
panel重新繪制
修改widthMeasureSpec和heightMeasureSpec的size為0姑裂,測(cè)量measure panel的child馋袜,就不會(huì)有寬高。作者利用了一個(gè)view在measure時(shí)把自己置gone舶斧,不會(huì)觸發(fā)requestLayout,避免抖動(dòng)欣鳖。
keyboard切換回panel
為了防止抖動(dòng)將setVisibility方法進(jìn)行重寫
此時(shí),調(diào)用KeyboardUtil.hideKeyboard(activity.getCurrentFocus())導(dǎo)致keyboard隱藏茴厉,把mIsHide設(shè)置為false(keyboard和panel的切換通過mIsHide進(jìn)行過渡)
// KPSwitchRootLayoutHandler.java
if (panel.isVisible()) { isVisible
// the panel is showing/will showing
panel.handleShow();
}
// KPSwitchPanelLayoutHandler.java
@Override
public boolean isVisible() {
return !mIsHide;
}