Панель навигации с представлением прокрутки Android

Это мой макет, который использует панель навигации и вид прокрутки.

<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:fitsSystemWindows="true"
tools:context=".AccountActivity">

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- your content layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:elevation="0dp">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                app:elevation="0dp" />

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

        <!--content of account activity-->

        <ScrollView
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            >

            <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:id="@+id/activity_setup"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".AccountActivity">

                <ImageView
                    android:layout_width="match_parent"
                    android:background="@drawable/listgrad"
                    android:id="@+id/imageView"
                    android:layout_height="100dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName"
                    android:text="Name"
                    android:layout_below="@+id/profilepic"
                    android:id="@+id/accountnamefield"
                    android:textSize="18sp"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Recently Match"
                    android:id="@+id/recentMatchTitle"
                    android:textSize="18sp"
                    android:layout_below="@+id/accountnamefield"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <android.support.v7.widget.RecyclerView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/recentMatchProductList"
                    android:layout_below="@+id/recentMatchTitle"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true">
                </android.support.v7.widget.RecyclerView>

                <ImageButton
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    app:srcCompat="@mipmap/ic_account_circle_white_36dp"
                    android:id="@+id/profilepic"
                    android:background="@drawable/round_button"
                    android:layout_marginTop="21dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />

            </RelativeLayout>

        </ScrollView>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/drawer" />

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

Я использую вид прокрутки с навигационным ящиком. Почему я открываю и закрываю навигационный ящик, и вид переходит в нижнее положение? Могу ли я переместить его наверх или оставить прежнюю позицию? Пожалуйста, дайте мне помощь.

2 ответа

Удалить android:inputType="textPersonName" часть первая для TextView.Затем добавьте android:scrollbars="vertical" на ваш ScrollView. Это работало нормально для меня.

Здесь я опубликую код, который я использовал для тестирования, ScrollView сохранял ту же позицию при использовании NavigationDrawer

<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:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- your content layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:elevation="0dp">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="?attr/actionBarSize"
                app:elevation="0dp" />

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

        <!--content of account activity-->

        <ScrollView
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:scrollbars="vertical"
            >

            <LinearLayout 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:id="@+id/activity_setup"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                tools:context=".MainActivity">


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/bla"
                    android:id="@+id/accountnamefield"
                    android:textSize="18sp"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Recently Match"
                    android:id="@+id/recentMatchTitle"
                    android:textSize="18sp"
                    android:layout_below="@+id/accountnamefield"
                    android:layout_centerVertical="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />



            </LinearLayout>

        </ScrollView>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

У вашего макета есть куча проблем:

android: layout_alignParentStart = "true" - то же самое, что слева (или справа для RTL) android:layout_alignParentEnd="true" - то же самое, что справа (или слева для RTL)

Неправильно объявлять старт и уходить одновременно

Использование представления переработчика внутри ScrollView - плохая практика и никогда не сработает. Вы можете использовать вложенное представление прокрутки ( RecyclerView внутри ScrollView не работает), но лучшее решение - поместить то, что вы хотите, в представление прокрутки (как верхние и нижние колонтитулы).

Я думаю, что нет способа измерить ваш макет. Попытайтесь выяснить, как Android рисует виды ( https://medium.com/@britt.barak/measure-layout-draw-483c6a4d2fab).

PS У вас гораздо больше проблем с вашим макетом, проще написать новый, чем указать все проблемы.