当键盘弹出时,CardView 的滚动行为

4
我们的应用主要内容是由CardView的RecyclerView组成的。为了注册,我们需要更多的信息来创建一个账户,因此我们决定使用CardView来匹配用户注册后的体验。
为此,我有一个单一的Activity,它会从底部动画地显示片段,并从顶部退出现有的片段,以模拟滚动。当用户输入数据并点击“下一步”时,这种虚假的滚动发生。这个方法效果不错,但有一个问题。当我们有一个用于输入的EditText时,键盘弹出并覆盖屏幕底部的“下一步”按钮。
在用户测试中,我们注意到有很高的比例的用户试图向上滚动卡片以达到下一步按钮,而不是关闭键盘。
我花费了很多时间尝试使CardView向上滚动以显示按钮,但没有成功,现在正在寻找新的解决方案。
注册Activity布局仅包含一个FrameLayout,我将Fragment加载到其中。每个被加载的Fragment都有一个CardView作为根布局。
在清单文件中,我将Activity的windowsSoftInputMode设置为adjustResize、adjustPan,但效果不佳。
activity_signup.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/signUpContent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

简化的fragment_enter_code.xml

<android.support.v7.widget.CardView
style="@style/CardViewStyle.SignUp"
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="wrap_content"
app:cardCornerRadius="25dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:contentPadding="8dp">

        <EditText
             android:id="@+id/codeEditText"
             style="@style/HintedEditText"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:hint="Code"
             android:inputType="text"/>

        <Button
            style="@style/NextButton"
            android:id="@+id/nextButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:text="@string/next"/>

</android.support.v7.widget.CardView>

当我试图将CardView放入ScrollView中并使用fillViewport true的cardview布局时,我得到了一个滚动条,但是卡片不会滚动,而且cardview布局也会混乱。
有人对windowSoftInputMode非常了解,能否指点我正确的方向?或者CardView只能在设计用于容纳它们的容器内滚动?
感觉解决这个问题的方法是操作活动视图而不是片段。
1个回答

7

我最终创建了一个新的应用程序来解决这个问题,并注意到,如果我的布局不包含根布局为ScrollView的CardView,它将无法滚动,除非activities的windowSoftInputMode设置为adjustResize,然后它才会滚动。

然后我创建了一个布局,其中包含<ScrollView><CardView><content...></CardView></ScrollView>,但是CardView的大小始终是卡片的默认行大小,并且不会match_parent。 我通过在ScrollView上使用fillViewPort =“ true”解决了这个问题,但是当键盘弹出时,它不会滚动。

事实证明,秘诀是在CardView和ScrollView之间放置FrameLayout(或任何其他布局)。

您仍然必须考虑调整布局的大小以防止视图元素重叠,但是一旦您这样做,您现在可以滚动软键盘上方的视图并能够使用CardView访问其余UI。

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardCornerRadius="35dp"
            app:cardElevation="2dp"
            app:cardUseCompatPadding="true"
            app:contentPadding="8dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:src="@drawable/common_ic_googleplayservices"/>

                <EditText
                    android:id="@+id/input"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="50dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:layout_below="@id/image"
                    android:hint="Input"
                    android:inputType="text"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/input"
                    android:orientation="vertical"
                    android:gravity="bottom|center_horizontal">

                    <Button
                        android:id="@+id/nextButton"
                        android:layout_width="match_parent"
                        android:layout_height="48dp"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="20dp"
                        android:enabled="false"
                        android:text="Next"/>

                </LinearLayout>

            </RelativeLayout>

        </android.support.v7.widget.CardView>

    </FrameLayout>

</ScrollView>

非常好!像魔法一样运行! - bazyle

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