scrolling in scrollview inside a fragment in ViewPager with soft keyboard visible

我只是一个虾纸丫 提交于 2019-12-11 00:33:50

问题


I use a ViewPager and inside the first fragment of the ViewPager I have a another fragment that is parenting a sub fragment with ScrollView in it. to make it more visual:

┌------------┐
|   1        | 1 is the ViewPager fragment
| ┌---------┐|
| | 2       || 2 is the fragment inside ViewPager fragment
| |┌-------┐||
| ||3      ||| 3 is the sub fragment containing the ScrollView with EditText form
| ||form   |||
| ||here   |||
| ||       |||
| |└-------┘||
| └---------┘|
└------------┘

problem: the view adjust but the scroll view cannot scroll till the end of the view and some of the content remains behind the keyboard. this behaviour changes when I select the edit texts from bottom of the form. in that case, the fragment 1 (viewpager) and fragment 2(sub fragment) move up and allow the scroll view to be show even the last element.

while searching for this behaviour I saw many posts on SO such as:

Scrollview inside a viewpager fragment doesn't scroll when keyboard is shown

and ofcourse the original bug report for fullscreen activities which has been marked as intentional behaviour. note that my case I am not using a full screen activity. but there has been reports of similar behaviour for ScrollView in ViewPager.

my attempt to use the Workaround:

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) {
    new AndroidBug5497Workaround(activity);
}

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

private AndroidBug5497Workaround(Activity activity) {
    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
            frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
        } else {
            // keyboard probably just became hidden
            frameLayoutParams.height = usableHeightSansKeyboard;
        }
        mChildOfContent.requestLayout();
        usableHeightPrevious = usableHeightNow;
    }
}

private int computeUsableHeight() {
    Rect r = new Rect();
    mChildOfContent.getWindowVisibleDisplayFrame(r);
    return (r.bottom - r.top);
}

}

The issue is when I use the workaround it works, but it will also create a extra white space that increases when I click on the lower EditTexts in the form inside scrollview. I suspect it has to do with adjust_resize or miscalculating the height in workaround.

here is the result of the of the workaround.

when I select an EditText from top of the form (little extra white space)

when I select an EditText from end of the Form (a lot of extra white space)

your helps are appreciated!


回答1:


Have you tried android:fillViewport="true" inside ScrollView if not then try this.

And use this in your manifiest's Activity which contain your ViewPager

android:windowSoftInputMode="adjustResize"

Hope it will help you.



来源:https://stackoverflow.com/questions/45328468/scrolling-in-scrollview-inside-a-fragment-in-viewpager-with-soft-keyboard-visibl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!