当显示键盘时,viewpager片段内的Scrollview不会滚动

时间:2016-02-01 23:14:43

标签: android android-edittext scrollview window-soft-input-mode

我有一个活动,其布局内容如下:

<?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>

在viewpager中,我有一个片段,它具有以下布局:

<?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>

当片段内的内容不适合屏幕时,滚动视图允许用户查看屏幕下方的内容。 (按预期工作)

问题是当edittext获得焦点时,键盘与edittext区域重叠,而不是滚动视图导致内容滚动。

我希望片段能够处理显示的键盘和滚动视图以适应屏幕上的键盘。

或者我错了,是否对键盘负责?由于活动布局中的根视图是linearlayout,它是否限制了扩展?

我尝试使用滚动视图包装活动布局的整个内容,但是如果我没有给它一个固定的大小,这次viewpager不会出现在屏幕上。 即使我这样做,我最终在片段内部有一个滚动视图,它位于主布局的滚动视图下方,而键盘仍然悬停在编辑文本上。

我在这里有点迷失,我的目标是能够在编辑文本聚焦时滚动片段内容,而当前键盘与edittext重叠。

4 个答案:

答案 0 :(得分:6)

我可能会误会,但可能是因为已知的Android错误,您只需要在项目中添加下一个类:

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

}

然后只需在包含assistActivity()ViewPager的活动中调用SlidingTabLayout方法即可使用它:

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

更多背景检查this主题。

答案 1 :(得分:0)

我在ScrollView中遇到类似的问题。您需要在OnTouchListener内添加与ScrollView相似的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;
    }
});

这是另一个SO Answer可能对您有所帮助。

答案 2 :(得分:0)

我认为在显示键盘时需要使用adjustPan或adjustResize来自动调整空间。 根据文件:

&#34; adjustResize&#34;活动的主窗口始终调整大小,以便为屏幕上的软键盘腾出空间。

&#34; adjustPan&#34;活动的主窗口未调整大小以便为软键盘腾出空间。相反,窗口的内容会自动平移,以便键盘不会遮挡当前焦点,用户可以随时看到他们正在键入的内容。这通常不如调整大小,因为用户可能需要关闭软键盘才能进入并与窗口的模糊部分进行交互。

您可以在活动代码下的清单中声明其中一个,如下所示: 机器人:windowSoftInputMode =&#34; stateUnchanged | adjustResize

我希望这能满足您的要求。

答案 3 :(得分:0)

您必须创建ViewPager的子类,然后覆盖onTouchEvent,如this

中所示