在 ViewPager 中的片段内的滚动视图中滚动,软键盘可见

2024-06-08

I use a ViewPager并在第一个片段内ViewPager我有另一个片段,它是子片段的父级ScrollView在里面。使其更加直观:

┌------------┐
|   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   |||
| ||       |||
| |└-------┘||
| └---------┘|
└------------┘

问题:视图调整但滚动视图无法滚动到视图末尾,并且某些内容保留在键盘后面。当我从表单底部选择编辑文本时,此行为会发生变化。在这种情况下,片段 1(viewpager)和片段 2(子片段)向上移动并允许滚动视图甚至显示最后一个元素。

在搜索这种行为时,我看到了很多帖子SO例如:

显示键盘时,viewpager 片段内的滚动视图不会滚动 https://stackoverflow.com/questions/35142251/scrollview-inside-a-viewpager-fragment-doesnt-scroll-when-keyboard-is-shown

当然还有原始错误报告 https://issuetracker.google.com/issues/36911528对于已被标记为故意行为的全屏活动。请注意,在我的情况下,我没有使用全屏活动。但有报道称,类似行为ScrollView in ViewPager.

我尝试使用解决方法:

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);
}

}

问题是,当我使用它的解决方法时,它会起作用,但它也会创建一个额外的空白,当我单击滚动视图内表单中的下部 EditText 时,该空白会增加。 我怀疑这与adjust_resize或在解决方法中错误计算高度。

这是解决方法的结果。

当我选择一个EditText从表格顶部开始(少量额外的空白)

当我选择一个EditText从表格末尾开始(大量额外的空白)

感谢您的帮助!


你有没有尝试过android:fillViewport="true" inside ScrollView如果没有那就试试这个。

并在您的清单的 Activity 中使用它,其中包含您的ViewPager

android:windowSoftInputMode="adjustResize"

希望它能帮助你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 ViewPager 中的片段内的滚动视图中滚动,软键盘可见 的相关文章

随机推荐