【问题标题】:How to scroll an EditText to the very top of the layout in a fragment?如何将 EditText 滚动到片段中布局的最顶部?
【发布时间】:2019-10-02 12:41:09
【问题描述】:

所以我有一个带有一些 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 下方。

【问题讨论】:

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


【解决方案1】:

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

或者你可以试试

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

【讨论】:

    【解决方案2】:

    在您的 onCreateView 方法中试试这个
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    希望对你有帮助。

    【讨论】:

    • 它没有作用,它被放置在键盘上方但我仍然没有足够的空间能够看到mi可过滤列表,我需要它到最顶部
    猜你喜欢
    • 2020-01-04
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多