【问题标题】:scrolling in scrollview inside a fragment in ViewPager with soft keyboard visible在 ViewPager 中的片段内滚动滚动视图,软键盘可见
【发布时间】:2017-07-26 13:30:03
【问题描述】:

我使用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 上看到了许多帖子,例如:

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

当然还有original bug report 用于标记为故意行为的全屏活动。请注意,我没有使用全屏活动。但有报道称ScrollViewViewPager 中有类似行为。

我尝试使用解决方法:

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 或解决方法中错误计算高度有关。

这是解决方法的结果。

当我从表单顶部选择一个EditText 时(一点额外的空白)

当我从表单末尾选择一个EditText 时(很多额外的空白)

感谢您的帮助!

【问题讨论】:

  • 你有什么解决办法吗?就我而言,我在视图寻呼机片段中有选项卡布局(在屏幕底部)。使用解决方法类我的选项卡布局正在消失并且出现空白问题。

标签: android android-viewpager scrollview android-softkeyboard


【解决方案1】:

您是否在ScrollView 中尝试过android:fillViewport="true",如果没有,请尝试此操作。

并在包含您的 ViewPager 的清单的活动中使用它

android:windowSoftInputMode="adjustResize"

希望对你有所帮助。

【讨论】:

  • 我尝试过使用adjustResizeadjustPan。当我使用adjustResize 时,它可以很好地滚动ScrollView。但在小屏幕中sub fragment 3 隐藏在soft keyboard 后面。当我在小屏幕上使用“adjustPan”时,它会将父片段 1 和 2 向上推并显示片段 3。但 ScrollView 不会滚动到最后,并且某些内容隐藏在键盘后面。 here I have also posted a question with more details on that subject感谢您的帮助
  • 为了进一步解释这个问题,我上传了一个包含相关代码的迷你项目,以促进该过程。如果你看一下Github Repo Link,我将不胜感激
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多