Android ViewPager显示软键盘时如何将布局移动到上方

5

好的,我已经在做(我相信)一切需要做的事情,以使我的应用程序布局在软键盘显示时滚动/移动,但它并不起作用,所以我猜想可能有些东西阻止了它,我想知道是不是我给我的 ViewPagers 设定的固定高度或者我不知道的其他东西,我已经阅读了描述相对布局在键盘显示时“压缩子视图”的帖子和线性布局不会“压缩子视图”的帖子,因此考虑到这一点,以下是我的布局:

主活动

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout    
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/viewpagerHolder">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager2"
            android:layout_width="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:layout_height="@dimen/card_pager_height" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/predictsHolder"
            android:layout_below="@id/viewpager2">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager_predicts"
            android:layout_width="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="4dp"
            android:layout_height="@dimen/predicts_pager_height" />

        </RelativeLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/predictsHolder"
            app:tabGravity="fill"
            android:theme="@style/CustomTabLayoutStyle" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tabs">


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:background="@color/windowBackground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             />

        </LinearLayout>

    </RelativeLayout>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/card_pager_height">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="12dp"
        android:layout_marginEnd="12dp"
        app:elevation="4dp"
        android:src="@drawable/ic_playlist_play_white_24dp"
        android:layout_gravity="right|top"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

总共有3个ViewPager,每个都有不同的布局,第一个(最顶部的)

第一个ViewPager

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"
android:paddingLeft="2dp"
android:paddingRight="2dp"
tools:context=".SpeakGridDB">

<android.support.v7.widget.RecyclerView
    android:id="@+id/card_speak_grid"
    android:layout_width="match_parent"
    android:layout_gravity="center"
    android:layout_height="wrap_content"/>

</RelativeLayout>

第二个ViewPager

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:background="@drawable/border">

<android.support.v7.widget.RecyclerView
    android:id="@+id/predicts_card_speak_grid"
    android:layout_width="match_parent"
    android:layout_gravity="center"
    android:layout_height="wrap_content"/>

</RelativeLayout>

第三个ViewPager

当键盘弹出时,我希望这个布局仍然存在。目前它位于布局的底部,我已将其放置在线性布局中,并尝试将其ViewPager放置在线性布局中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/windowBackground"
tools:context=".OneFragment">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/activity_main_swipe_refresh_layout"
    android:layout_width="match_parent"
    android:background="@color/windowBackground"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_grid"
        android:background="@color/windowBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"/>

</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

我也在使用

        android:windowSoftInputMode="adjustResize"

我在清单文件中尝试了以下操作:

        android:windowSoftInputMode="adjustPan"

看起来当键盘出现时,TabLayout会稍微向上移动,但通常顶部两个视图保持原位,底部一个(我想要显示的)被键盘遮挡。

任何帮助都将不胜感激。

更新 我仍然没有解决这个问题。我可以创建一个固定高度的布局,并使其在软键盘出现时完美地滚动,所以这不是问题,但我必须在元素上使用布局中心垂直,这不是我想要做的。我还尝试将所有内容包装在ScrollView中并使用isScrollContainer true,但仍然无济于事。有人有什么想法吗?


当键盘弹出时,FAB是否向上移动? - Darshan Miskin
@darshan miskin 不是的,我也不希望它这样做。风扇在布局中相当高,不应受影响。 - Martin Seal
2个回答

4

我可能错了,但这个问题的原因很可能是已知的Android bug。

因此,首先需要在<activity>标签中添加android:windowSoftInputMode="stateHidden|adjustResize"

其次,您需要将此类添加到您的项目中:

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

}

然后,在您持有ViewPagers的MainActivity中调用assistActivity()方法即可简单使用它:

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

For more background check this thread.


嗨@rom4ek,首先感谢您,这个类的名字真是太棒了,哈哈。我已经将它插入了,当键盘显示时,布局会发生变化,但不是我想要的样子。当键盘隐藏时,布局会恢复正常。您创建了这个吗?如果是,我需要调整什么才能使其完美?我猜测是在这一行frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;上需要进行调整。现在我只能得到一个空白屏幕。 - Martin Seal
当你的键盘显示时,屏幕是空白的,对吗? - romtsn
不是空白,抱歉,实际上是我的布局背景持有了viewpager。我将该行更改为frameLayoutParams.height = usableHeightSansKeyboard - heightDifference/2; 现在我仍然可以看到我的布局顶部的一半(显然)。 - Martin Seal
我仍然没有想出一个可行的解决方案,但是我已经将你的答案选为正确答案,因为这可能是我的解决方法的开端。感谢你的时间和努力。 - Martin Seal
谢谢。除了我的答案之外,还有一种方法 - 尝试使用android.support.design.internal.ScrimInsetsFrameLayout代替具有滚动行为的RelativeLayout。抱歉没有足够的时间深入研究你的问题。 - romtsn

0
你可以尝试在协调布局中添加android:fitsSystemWindows="true"

一些关于它的作用和应该添加在哪里的评论会很有帮助。 - Jonathan Mee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接