【发布时间】: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 用于标记为故意行为的全屏活动。请注意,我没有使用全屏活动。但有报道称ScrollView 在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 或解决方法中错误计算高度有关。
这是解决方法的结果。
当我从表单顶部选择一个EditText 时(一点额外的空白)
当我从表单末尾选择一个EditText 时(很多额外的空白)
感谢您的帮助!
【问题讨论】:
-
你有什么解决办法吗?就我而言,我在视图寻呼机片段中有选项卡布局(在屏幕底部)。使用解决方法类我的选项卡布局正在消失并且出现空白问题。
标签: android android-viewpager scrollview android-softkeyboard