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
从表格末尾开始(大量额外的空白)
感谢您的帮助!