我使用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);
}
}
问题是当我使用它可以使用的变通方法时,但是当我单击scrollview中的表单中的较低EditTexts时,它还会创建一个额外的空白区域。
我怀疑它与adjust_resize
有关,或者在解决方法中错误计算高度。
这是解决方法的结果。
当我从表单顶部选择EditText
时(小额外的空白区域)
当我从表格的末尾选择EditText
时(很多额外的空格)
感谢您的帮助!
答案 0 :(得分:0)
您是否在android:fillViewport="true"
内尝试ScrollView
,如果没有,请尝试此操作。
并在包含ViewPager
android:windowSoftInputMode="adjustResize"
希望它会对你有所帮助。