2015-08-19 113 views
4

我正在開發一個應用程序,其中包含活動和Fragment。在片段佈局中,我使用相對佈局作爲父佈局,一個按鈕位於底部和滾動視圖之間。 Scrollview包含editText框。如果我在scrollview中點擊最後的editTextBox我的鍵盤隱藏了fragment。我在清單中嘗試了adjustpan|adjustresize,並且在我的fragment中嘗試過,但尚未解決問題。安卓鍵盤調整大小

+0

你試過[這個答案](http://stackoverflow.com/a/ 34563274/2826147) –

回答

0

這裏有Android的錯誤。在掙扎了很多之後,我能夠順利解決這個問題。這是一個單線解決方案,但它有一些預先要求。一條線是:

AndroidBug5497Workaround.assistActivity(this, R.id.LayoutInScrollView); 

你的XML佈局必須是這樣的:

RelativeLayout{ 

HeaderView{} 

ScrollView{ 
    LinearLayout{ 
    @+id/LayoutInScrollView 
    } 
} 

FooterView{}  // the buttons u want to appear above keyboard 
} 

如果你不使用全屏幕,下面的類應該足夠:

class AndroidBug5497Workaround{ 

    View svChildLayout; 
    int originalGravity; 
    Activity activity; 

    /** 
    * @param activity 
    * @param svChildLayoutId id of the layout that is the first child of the center ScrollView 
    */ 
    public static void assistActivity (Activity activity, int svChildLayoutId) { 
     new AndroidBug5497Workaround(activity, svChildLayoutId); 
    } 


    private AndroidBug5497Workaround(Activity activity, int svChildLayoutId) { 

     this.activity = activity; 
     svChildLayout = activity.findViewById(svChildLayoutId); 
     originalGravity = ((ScrollView.LayoutParams)svChildLayout.getLayoutParams()).gravity; 

     //Add listener 
     FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); 
     mChildOfContent = content.getChildAt(0); 
     mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      public void onGlobalLayout() { 
       possiblyResizeChildOfContent2(); 
      } 
     }); 

    } 
    private void possiblyResizeChildOfContent2() { 
     int usableHeightNow = computeUsableHeight(); 
     if (usableHeightNow != usableHeightPrevious) { 
      int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); 
      int heightDifference = usableHeightSansKeyboard - usableHeightNow; 
      if (heightDifference > (usableHeightSansKeyboard/4)) { 
       // keyboard probably just became visible 
       onKeyboardVisible(); 
      } else { 
       // keyboard probably just became hidden 
       onKeyboardHidden(); 
      } 
      usableHeightPrevious = usableHeightNow; 
     } 
    } 


    private void onKeyboardVisible() { 

     ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams(); 
     params.gravity = Gravity.TOP; 
     svChildLayout.requestLayout(); 

     final ScrollView parentSv = (ScrollView) svChildLayout.getParent(); 
     parentSv.post(new Runnable() { 
      @Override 
      public void run() { 
       View focusedEditText = activity.getWindow().getCurrentFocus(); 
       parentSv.smoothScrollTo(0, focusedEditText.getTop()); 
      } 
     }); 
    } 

    private void onKeyboardHidden() { 
     ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams(); 
     params.gravity = originalGravity; 
     svChildLayout.requestLayout(); 
    } 
} 

如果您使用全屏幕,您需要以下類別(從windowSoftInputMode="adjustResize" not working with translucent action/navbar修改):

public class AndroidBug5497Workaround { 

     // For more information, see https://code.google.com/p/android/issues/detail?id=5497 
     // To use this class, simply invoke assistActivity() on an Activity that already has its content view set. 

     public static void assistActivity (Activity activity, int svChildLayoutId) { 
      new AndroidBug5497Workaround(activity, svChildLayoutId); 
     } 

     private View mChildOfContent; 
     private int usableHeightPrevious; 
     private FrameLayout.LayoutParams frameLayoutParams; 

     View svChildLayout; 
     int originalGravity; 
     Activity activity; 

     private AndroidBug5497Workaround(Activity activity, int svChildLayoutId) { 


     this.activity = activity; 
     svChildLayout = activity.findViewById(svChildLayoutId); 
     originalGravity = ((ScrollView.LayoutParams)svChildLayout.getLayoutParams()).gravity; 

      FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); 
      mChildOfContent = content.getChildAt(0); 
      mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
       public void onGlobalLayout() { 
        possiblyResizeChildOfContent(); 
       } 
      }); 
      frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); 
     } 

     private void possiblyResizeChildOfContent() { 
      int usableHeightNow = computeUsableHeight(); 
      if (usableHeightNow != usableHeightPrevious) { 
       int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); 
       int heightDifference = usableHeightSansKeyboard - usableHeightNow; 
       if (heightDifference > (usableHeightSansKeyboard/4)) { 
        // keyboard probably just became visible 
onKeyboardVisible();      
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference; 
       } else { 
        // keyboard probably just became hidden 
onKeyboardHidden();      
frameLayoutParams.height = usableHeightSansKeyboard; 
       } 
       mChildOfContent.requestLayout(); 
       usableHeightPrevious = usableHeightNow; 
      } 
     } 

     private int computeUsableHeight() { 


       Rect r = new Rect(); 
       mChildOfContent.getWindowVisibleDisplayFrame(r); 
       return (r.bottom - r.top); 
      } 

private void onKeyboardVisible() { 

     ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams(); 
     params.gravity = Gravity.TOP; 
     svChildLayout.requestLayout(); 

     final ScrollView parentSv = (ScrollView) svChildLayout.getParent(); 
     parentSv.post(new Runnable() { 
      @Override 
      public void run() { 
       View focusedEditText = activity.getWindow().getCurrentFocus(); 
       parentSv.smoothScrollTo(0, focusedEditText.getTop()); 
      } 
     }); 
    } 

    private void onKeyboardHidden() { 
     ScrollView.LayoutParams params = (ScrollView.LayoutParams) svChildLayout.getLayoutParams(); 
     params.gravity = originalGravity; 
     svChildLayout.requestLayout(); 
    } 
    } 
+0

感謝您的回覆 – Surendran

+0

@usman不錯的解決方案。我面臨類似的問題,我嘗試使用類似的解決方法,但它不工作。我也嘗試過使用您的自定義解決方法,但沒有全屏活動,但沒有奏效。此外,我嘗試了全屏版本,並在鍵盤上方創建了一個額外的空白區域。我已經發布了我的問題更多的細節[這裏](https://stackoverflow.com/questions/45328468/scrolling-in-scrollview-inside-a-fragment-in-viewpager-with-soft-keyboard-visibl)我會感謝你的幫助。謝謝 – Poorya

1

試試這個AndroidMenifest

android:windowSoftInputMode="stateAlwaysHidden|adjustResize 

在你editext使用本

android:inputType="textMultiLine|textPostalAddress" 
android:scrollbars="vertical" 
+0

不是你的問題解決?@Surendran –