Лучший способ сохранить положение прокрутки вида - PullRequest
0 голосов
/ 06 июля 2018

У меня длинный вид, который содержит несколько различных макетов. Каков наилучший способ сохранить положение прокрутки после изменения ориентации устройства или возврата из другого занятия и т. Д.? Он работает "из коробки", если мы используем ListView или RecyclerView, но, как я уже сказал, у меня есть только View.

Ответы [ 3 ]

0 голосов
/ 06 июля 2018

Просто поместите ScrollView в качестве родителя для других макетов

Здесь у вас есть пример одного из моих проектов, где у меня есть представление переработчика и ConstraintLayout внутри Scrollview

<ScrollView 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"
    android:background="#134A80"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout 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="#134A80"
        tools:context=".UltimasConsultasActivity">


        <EditText
            android:id="@+id/casilla_email_rv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:hint="Ingrese email"
            android:textColor="#FFF"
            android:textColorHint="#FFF"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.501"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/itemsSeleccionadosList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"

            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"


            android:text="Items seleccionados: "
            android:textColor="#FFF"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/casilla_email_rv"
            app:layout_constraintVertical_bias="0.026" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="#134A80"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/itemsSeleccionadosList">

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




    </android.support.constraint.ConstraintLayout>
</ScrollView>
0 голосов
/ 06 июля 2018
 <?xml version="1.0" encoding="utf-8"?>
    <ScrollView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name"
                android:textColor="@color/colorPrimaryDark" />

            <EditText
                android:id="@+id/name"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:layout_marginStart="30dp" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="SurName"
                android:textColor="@color/colorPrimaryDark" />

            <EditText
                android:id="@+id/surname"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:layout_marginStart="10dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Marks"
                android:textColor="@color/colorPrimaryDark" />

            <EditText
                android:id="@+id/marks"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:layout_marginStart="30dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ID"
                android:textColor="@color/colorPrimaryDark" />

            <EditText
                android:id="@+id/id"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:layout_marginStart="60dp" />
        </LinearLayout>

        <Button
            android:id="@+id/add"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Add" />

        <Button
            android:id="@+id/showdata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Showdata" />

        <Button
            android:id="@+id/updatedata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="update" />
        <Button
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="delet" />

    </LinearLayout>
    </ScrollView>
0 голосов
/ 06 июля 2018

Что вы имеете в виду, у вас есть только вид? Скроллвью?

Если это так, я бы предложил вам использовать фрагменты вместо действий и добавлять их в backstack при переключении.

...