1. 程式人生 > >adjustResize模式下ExpandaleListView中輸入框焦點錯亂及佈局底部的導航欄被頂在鍵盤上方的處理

adjustResize模式下ExpandaleListView中輸入框焦點錯亂及佈局底部的導航欄被頂在鍵盤上方的處理

  為了更好的使用者體驗,煎熬了許久,得到這麼個解決方案。在此記錄下來,以供後來者參考。

  第一部分

    清單檔案中元件activity的android:windowSoftInputMode屬性值的含義: 

    【A】stateUnspecified:軟鍵盤的狀態並沒有指定,系統將選擇一個合適的狀態或依賴於主題的設定

    【B】stateUnchanged:當這個activity出現時,軟鍵盤將一直保持在上一個activity裡的狀態,無論是隱藏還是顯示

    【C】stateHidden:使用者選擇activity時,軟鍵盤總是被隱藏

    【D】stateAlwaysHidden:當該Activity主視窗獲取焦點時,軟鍵盤也總是被隱藏的

    【E】stateVisible:軟鍵盤通常是可見的

    【F】stateAlwaysVisible:使用者選擇activity時,軟鍵盤總是顯示的狀態

    【G】adjustUnspecified:預設設定,通常由系統自行決定是隱藏還是顯示

    【H】adjustResize:該Activity總是調整螢幕的大小以便留出軟鍵盤的空間

    【I】adjustPan:當前視窗的內容將自動移動以便當前焦點從不被鍵盤覆蓋

   注意:

      1、當對狀態列進行沉浸式處理時,adjustResize和adjustPan模式可能會失效,此時應該給activity的根佈局設定此屬性:android:fitsSystemWindows="true"。

      2、adjustResize模式下,要想鍵盤不遮擋輸入框,那麼就需要使用scrollview來包裹,如果editext所在的直屬scrollview下面還有控制元件(多數時候是一個或者多個按鈕),那麼鍵盤上將會出現這些按鈕,體驗不好;adjustPan模式下,會將佈局向上頂出螢幕,以保證輸入框始終在視野範圍內,所以title會被頂出,而體驗差。

  第二部分

    對軟鍵盤的顯示和隱藏狀態進行監聽。目的是解決adjustResize模式下,佈局下面的一些懸浮按鈕顯示在鍵盤上的問題。

   DecorView decorview=getWindow().getDecorView();

  onGlobalLayoutListener=new ViewTreeObserver.OnGlobalLayoutListener() {
  @Override
   public void onGlobalLayout() {
   Rect rect = new Rect();
   decorview.getWindowVisibleDisplayFrame(rect);
   displayHight = rect.bottom - rect.top;//佈局當下的高度
   hight = decorview.getHeight();//螢幕高度
   if (displayHight > hight / 3 * 2) {
          //佈局當下的高度大於屏高的2/3,那麼鍵盤處於隱藏的狀態
          } else {
          //佈局當下的高度小於等於屏高的2/3,那麼鍵盤處於顯示的狀態
          }
   }
  };
  decorview.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);

第三部分
  
  當adjustResize模式下,使用到
ExpandaleListView控制元件,且其分組展開項中包含有輸入框。這時會出現諸如:輸入框依舊被鍵盤遮擋了;遮擋的輸入框沒有焦點而不能輸入內容;展開和收起分組時自動滾動到焦點所在位置等問題。
  完整的處理方式如下:
    第一步:
      在
ExpandaleListView控制元件外層套上ScorllView。
      
<ScrollView
          android:id="@+id/sv"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scrollbars="none">

    <com.ruitukeji.bellink.view.NestedExpandaleListView
    android:id="@+id/exListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:childIndicator="@null"
    android:descendantFocusability="beforeDescendants"//這個屬性是決定輸入框能不能獲取到焦點
    android:divider="@null"
    android:dividerHeight="0dp"
    android:fastScrollEnabled="false"//這個屬性是跟巢狀時滾動相關的
    android:groupIndicator="@null" />
      </ScrollView>

      ScrollView巢狀
ExpandaleListView時,需要自定義ExpandaleListView控制元件,如下:
        public class NestedExpandaleListView extends ExpandableListView {

       public NestedExpandaleListView(Context context) {
       super(context);
       }

       public NestedExpandaleListView(Context context, AttributeSet attrs) {
       super(context, attrs);
       }

       public NestedExpandaleListView(Context context, AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
       }

       public NestedExpandaleListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
       super(context, attrs, defStyleAttr, defStyleRes);
       }

       @Override
       protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

       int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,

  MeasureSpec.AT_MOST);

       //將重新計算的高度傳遞回去
       super.onMeasure(widthMeasureSpec, expandSpec);
       }
      }
    第二步:
      給
ExpandaleListView控制元件設定展開和收起分組的監聽事件
      exListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
          public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
      exListView.requestFocus();//將焦點從輸入框奪回,避免自動滾動到輸入框所在的分組。並且需要把焦點給到
ExpandaleListView控制元件。當把焦點給到這裡的v時,這裡的監聽事件將無法再被執行。
              return false;//true:已經處理的點選事件,那麼分組的張開和收起將不會執行,除非,在此句之前通過程式碼來操作。false:沒有處理點選事件,系統自動執行展開或者收起操作。
      }
      });
    第三步:
      因為當收起鍵盤時,焦點還在輸入框的話,那麼此時點選分組進行展開或者收起操作,依舊會滾動到焦點所在位置,且頂佈局頂顯示。這個時候就需要我們去通過監聽鍵盤隱藏來將焦點從輸入框奪來給到
ExpandaleListView控制元件。
    注意:
ExpandaleListView有輸入框時,佈局儘可能不去複用。必須複用時,自行進行完善。


說明:本文是以ExpandaleListView控制元件來作為示例的,其它控制元件雷同,可舉一反三而得解決。
以上內容僅供參考,如有問題,煩請留言,共同提高。
鳴謝:
  https://blog.csdn.net/zhuawalibai/article/details/76668047