Scrollview inside viewpager snippet doesn't scroll when keyboard is displayed

I have an activity that has the contents of a layout as follows:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" style="@style/ToolBarStyle.Event" /> <com.mypackage.corecode.ui.view.SlidingTabLayout android:id="@+id/sliding_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primary_color" /> <android.support.v4.view.ViewPager android:id="@+id/vendor_main_tabview_pager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> 

And inside the viewpager, I have a fragment that has the following layout:

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:weightSum="3" android:orientation="horizontal"> <EditText android:theme="@style/ScrollableContentThemeNoActionBar" android:id="@+id/edit_text_vendor_sms_phone_number" android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content" android:inputType="number" /> <Button android:id="@+id/button_send_sms" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </ScrollView> 

When the content inside the fragment does not fit the screen, scrollview allows the user to see the content below the screen. (Works as expected)

The problem is that edittext gets focus, the keyboard overlaps the edittext area, not the scroll, causing the content to scroll.

I expect the fragment to process the displayed keyboard and scroll according to the keyboard on the screen.

Or am I mistaken and responsible for the keyboard? Since the root view in the activity layout is linearlayout, does it limit the extension?

I tried to wrap the entire content of the activity layout with scrollview, but this time the viewpager will not appear on the screen unless I give it a fixed size. Even when I do this, I get a scrollview inside the fragment that is under the scroll of the main layout, and still the keyboard is hovering over the edittext.

I got a little lost here, my goal is to scroll through the contents of the fragment when the edittext is focused, but the keyboard is overlapping the edittext at the moment.

+5
source share
4 answers

I could be wrong, but probably due to a known Android bug, all you need to do is add the following class to your project:

 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); } } 

And then just use it by calling the assistActivity() method in your activity, which contains ViewPager and SlidingTabLayout :

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(...); AndroidBug5497Workaround.assistActivity(this); ... } 

For additional background check this thread.

+6
source

I had a similar problem with ListView inside ScrollView. You need to add a similar OnTouchListener using ScrollView inside OnCrateView .

 myList.setOnTouchListener(new ListView.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: // Disallow ScrollView to intercept touch events. v.getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_UP: // Allow ScrollView to intercept touch events. v.getParent().requestDisallowInterceptTouchEvent(false); break; } // Handle ListView touch events. v.onTouchEvent(event); return true; } }); 

Here is another SO answer that might help you.

0
source

I think you need to use adjustPan or adjustResize to automatically adjust the space when the keyboard is displayed. According to the document:

"adjustResize" The main activity window is always resized to make room for the on-screen keyboard.

"adjustPan" The main operation window does not change to make room for the soft keyboard. Rather, the contents of the window automatically expand so that the current focus is never hidden by the keyboard, and users can always see what they are typing. This is generally less desirable than resizing, as the user may need to close the soft keyboard to receive and interact with the shaded parts of the window.

You can declare one of them in your manifest in the activity tag as follows: android: windowSoftInputMode = "stateUnchanged | adjustResize

I hope this will cover your demand.

0
source

You need to subclass the ViewPager class and then override onTouchEvent, as shown in this

0
source

Source: https://habr.com/ru/post/1242054/


All Articles