当软键盘出现时,Fragment中的滚动视图无法滚动。

5

我有一个Fragment,其中包含一个ScrollView

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/appbar">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.tatar.mobile.widget.CardSelectionView
            android:id="@+id/card_selection_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            app:accountRightTextView1="@string/available"
            app:accountTitleTextView="@string/from"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/line_text_view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="8dp"
            android:background="@color/gray.light"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/card_selection_view" />

        <Spinner
            android:id="@+id/type_spinner"
            style="@style/spinner"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/line_text_view" />

        <Spinner
            android:id="@+id/company_spinner"
            style="@style/spinner"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/type_spinner" />

        <LinearLayout
            android:id="@+id/dynamic_form_container"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/company_spinner" />

        <Spinner
            android:id="@+id/installment_spinner"
            style="@style/spinner"
            android:layout_marginTop="8dp"
            android:visibility="invisible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/dynamic_form_container" />

        <Button
            android:id="@+id/continue_button"
            style="@style/action_button"
            android:layout_marginTop="16dp"
            android:text="@string/continue_button_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/installment_spinner" />

    </android.support.constraint.ConstraintLayout>

</ScrollView>

ScrollView 中 ID 为 dynamic_form_containerLinearLayout 包含表单输入字段,这些字段将通过 Web 服务调用进行填充,根据发送到 Web 服务的参数,可能会有多达 6-7 个输入字段。因此,表单的高度相当高,这就是问题出现的地方。当我尝试向一个输入字段中输入内容时,软键盘会弹出,而其他一些输入字段则停留在软键盘下面,并且无法滚动。

我尝试将其添加到FragmentActivity清单中,但没有帮助。

android:windowSoftInputMode="adjustResize|stateHidden"

当软键盘弹出时,我希望能够滚动屏幕。我该如何解决这个问题?

非常感谢您的帮助。

3个回答

7
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

onCreateView方法中对我有用。

1
我已经找到了这个问题的解决方案在此处。将这个类包含到你的项目中。 并且在包含Fragment的Activity的onCreate方法中添加以下行:new KeyboardUtil(this,findViewById(R.id.fragment));

这在任何情况下都有效,只需传递您的视图即可。我尝试了具有一个editText和button的BottomSheetDialog,它也可以工作。 - Atul Bhardwaj

-1

你好,请参考下面的代码

这段代码对我来说是有效的。

<RelativeLayout 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"
android:background="@color/white">

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    </android.support.constraint.ConstraintLayout>
</ScrollView>

AndroidManifest.xml

  <activity
        android:name=".ui.activities.Activity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />

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