在ViewPager的片段内在scrollview中滚动并可见软键盘 [英] scrolling in scrollview inside a fragment in ViewPager with soft keyboard visible

查看:99
本文介绍了在ViewPager的片段内在scrollview中滚动并可见软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个ViewPager,在ViewPager的第一个片段内,我还有另一个片段,该片段将其中带有ScrollView的子片段作为父片段.使其更具视觉效果:

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

问题:视图调整,但滚动视图无法滚动直到视图结束,并且某些内容保留在键盘后面.当我从窗体底部选择编辑文本时,此行为会更改.在这种情况下,片段1(viewpager)和片段2(子片段)将向上移动并允许滚动视图甚至显示最后一个元素.

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.

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

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

viewpager片段内的滚动视图没有"在显示键盘时滚动

,当然还有用于全屏活动的原始错误报告,该行为已标记为故意行为.请注意,我的情况不是使用全屏活动.但是有报道称ViewPager中的ScrollView具有类似的行为.

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.

我尝试使用替代方法:

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

}

问题是,当我使用解决方法时,它会起作用,但是当我单击滚动视图内窗体中较低的EditTexts时,它也会创建一个额外的空白,该空白会增加. 我怀疑这与adjust_resize有关,或者在解决方法中错误地计算了高度.

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.

这是解决方法的结果.

当我从表单顶部选择EditText时(很小的空白)

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

当我从表单末尾选择EditText时(很多多余的空白)

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

我们非常感谢您的帮助!

推荐答案

是否在ScrollView中尝试过android:fillViewport="true",请尝试一下.

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

并在包含您的ViewPager

android:windowSoftInputMode="adjustResize"

希望它会对您有所帮助.

Hope it will help you.

这篇关于在ViewPager的片段内在scrollview中滚动并可见软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆