如何在片段中将EditText滚动到布局的最顶部?

时间:2019-10-02 12:41:09

标签: android android-layout android-fragments scrollview android-softkeyboard

所以我有一个带有一些EditTexts和ChipsInputs的活动,我希望它们在活动时滚动到最顶部,以便我的可过滤列表有显示空间。它与以下结构完美配合。问题在于,现在我需要将其转换为片段,但是在片段中,它不会表现出相同的行为。如何在片段中获得相同的结果?

这在一项活动中有效:

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background1"
        android:orientation="vertical">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:paddingHorizontal="5dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                tools:targetApi="o">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginHorizontal="12dp"
                    android:layout_marginVertical="5dp"
                    android:background="@color/black" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginLeft="10dp"
                        android:fontFamily="@font/quattrocento"
                        android:text="@string/about_me"
                        android:textColor="@color/textColor2"
                        android:textSize="20sp" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/white"
                        android:orientation="vertical">

                        <EditText
                            android:id="@+id/description"
                            android:layout_width="match_parent"
                            android:layout_height="70dp"
                            android:autofillHints="no"
                            android:background="@color/white"
                            android:inputType="text"
                            android:maxLength="500"
                            tools:ignore="LabelFor" />

                        <TextView
                            android:id="@+id/caracCounter"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="end"
                            android:fontFamily="@font/quattrocento"
                            android:text="500"
                            android:textColor="@color/black"
                            android:textSize="16sp" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:fontFamily="@font/quattrocento"
                        android:gravity="center"
                        android:text="@string/interest1"
                        android:textColor="@color/textColor2"
                        android:textSize="20sp" />

                    <Spinner
                        android:id="@+id/categories1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="10dp"
                        android:background="@color/colorPrimary"
                        android:gravity="center" />

                    <com.pchmn.materialchips.ChipsInput
                        android:id="@+id/chipsInput1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/white"
                        android:elevation="3dp"
                        app:chip_backgroundColor="@color/colorPrimary"
                        app:chip_deletable="true"
                        app:chip_deleteIconColor="@color/black"
                        app:chip_hasAvatarIcon="false"
                        app:chip_labelColor="@color/black"
                        app:filterable_list_backgroundColor="@color/background1"
                        app:filterable_list_textColor="@color/colorPrimary"
                        app:hint="#"
                        app:maxRows="3"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:fontFamily="@font/quattrocento"
                        android:gravity="center"
                        android:text="@string/interest2"
                        android:textColor="@color/textColor2"
                        android:textSize="20sp" />

                    <Spinner
                        android:id="@+id/categories2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="10dp"
                        android:background="@color/colorPrimary"
                        android:gravity="center" />

                    <com.pchmn.materialchips.ChipsInput
                        android:id="@+id/chipsInput2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="15dp"
                        android:background="@color/white"
                        android:elevation="3dp"
                        app:chip_backgroundColor="@color/colorPrimary"
                        app:chip_deletable="true"
                        app:chip_deleteIconColor="@color/black"
                        app:chip_hasAvatarIcon="false"
                        app:chip_labelColor="@color/black"
                        app:filterable_list_backgroundColor="@color/background1"
                        app:filterable_list_textColor="@color/colorPrimary"
                        app:hint="#" />
                </LinearLayout>

                <Button
                    android:id="@+id/btLogout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginBottom="5dp"
                    android:background="@color/colorPrimary"
                    android:elevation="12dp"
                    android:fontFamily="@font/quattrocento"
                    android:onClick="logout"
                    android:text="@string/logout"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    tools:targetApi="lollipop" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

我应该怎么做才能在片段中获得相同的结果?键盘覆盖了整个可过滤列表,该列表显示在EditText下方。

2 个答案:

答案 0 :(得分:2)

使用您的 onCreateView 方法进行尝试
 getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

希望对您有帮助。

答案 1 :(得分:0)

您需要致电editText.requestFocus();
这样活动就可以滚动到editText

或者您可以尝试

nestedScrollView.fling(0);
nestedScrollView.smoothScrollTo(0, 0);