显示软键盘后剪切的android scrollview
我对片段中的滚动视图有问题。我的平板电脑应用程序包含两个主要片段,一个用于左侧的菜单,另一个位于右侧,其中包含editText对象和一些其他内容。 当软键盘显示时,我试图垂直滚动右片段的内容 右侧片段中的内容滚动正在工作,但滚动后,该片段似乎在显示软键盘时系统栏出现的顶部和底部被剪切 在键盘被invalidate()和requestLayout()隐藏后,我尝试刷新scrollview,但没有成功。显示软键盘后剪切的android scrollview,android,android-fragments,android-edittext,scrollview,Android,Android Fragments,Android Edittext,Scrollview,我对片段中的滚动视图有问题。我的平板电脑应用程序包含两个主要片段,一个用于左侧的菜单,另一个位于右侧,其中包含editText对象和一些其他内容。 当软键盘显示时,我试图垂直滚动右片段的内容 右侧片段中的内容滚动正在工作,但滚动后,该片段似乎在显示软键盘时系统栏出现的顶部和底部被剪切 在键盘被invalidate()和requestLayout()隐藏后,我尝试刷新scrollview,但没有成功。 任何帮助都将不胜感激。提前感谢。使用customScrollView解决了此问题。重要的部分
任何帮助都将不胜感激。提前感谢。使用customScrollView解决了此问题。重要的部分是重载onApplyWindowInsets()函数
public class CustomScrollView extends ScrollView {
public CustomScrollView(Context pContext, AttributeSet pAttrs, int pDefStyle) {
super(pContext, pAttrs, pDefStyle);
}
public CustomScrollView(Context pContext, AttributeSet pAttrs) {
super(pContext, pAttrs);
}
public CustomScrollView(Context pContext) {
super(pContext);
}
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
if(insets.getSystemWindowInsetBottom() < 100){
WindowInsets newInset = insets.replaceSystemWindowInsets(new Rect(0,0,0,0));
Log.d("TEST","Keyboard is down");
return super.onApplyWindowInsets(newInset);
} else{
Log.d("TEST","Keyboard is up");
return super.onApplyWindowInsets(insets);
}
}
}
公共类CustomScrollView扩展了ScrollView{
公共自定义滚动视图(上下文pContext、属性集patters、int-pDefStyle){
超级(pContext、patters、pDefStyle);
}
公共自定义滚动视图(上下文pContext、属性集patters){
超级(pContext,patters);
}
公共自定义滚动视图(上下文pContext){
超级(pContext);
}
@凌驾
公共窗口插页在应用窗口插页上(窗口插页插页){
if(insets.getSystemWindowInsertBottom()<100){
WindowInsets newInset=insets.replaceSystemWindowInsets(新的Rect(0,0,0,0));
Log.d(“测试”,“键盘关闭”);
返回super.onapplywindowinset(newInset);
}否则{
Log.d(“测试”,“键盘启动”);
返回super.onApplyWindowInsets(insets);
}
}
}
为什么要以编程方式检测键盘和移动scrollview?在清单文件的活动标记中使用软输入窗口=调整平移,而不从java代码输入窗口中检测键盘=调整平移在清单中不允许我在键盘显示所需内容时滚动视图。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
// r will be populated with the coordinates of your view
// that area still visible.
rootView.getWindowVisibleDisplayFrame(r);
int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 300) { // if more than 100 pixels, its probably a keyboard...
Log.d(LOG_TAG, "keyboard shows up");
scrollToCurrentFocusedView();
}else{
Log.d(LOG_TAG, "keyboard vanishes");
//try to refresh the scrollView but not working
//scrollView.invalidate()
//scrollView.requestLayout()
}
}
});
return rootView;
}
protected void scrollToCurrentFocusedView(){
Log.d(LOG_TAG, "calling scrollToCurrentFocusedView");
View view = getActivity().getCurrentFocus();
if (view != null && scrollView != null) {
scrollView.smoothScrollTo(0, view.getBottom()-(view.getHeight()*3));
}
}
public class CustomScrollView extends ScrollView {
public CustomScrollView(Context pContext, AttributeSet pAttrs, int pDefStyle) {
super(pContext, pAttrs, pDefStyle);
}
public CustomScrollView(Context pContext, AttributeSet pAttrs) {
super(pContext, pAttrs);
}
public CustomScrollView(Context pContext) {
super(pContext);
}
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
if(insets.getSystemWindowInsetBottom() < 100){
WindowInsets newInset = insets.replaceSystemWindowInsets(new Rect(0,0,0,0));
Log.d("TEST","Keyboard is down");
return super.onApplyWindowInsets(newInset);
} else{
Log.d("TEST","Keyboard is up");
return super.onApplyWindowInsets(insets);
}
}
}