Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 保持视图滚动位置的最佳方法_Android_Scroll_View - Fatal编程技术网

Android 保持视图滚动位置的最佳方法

Android 保持视图滚动位置的最佳方法,android,scroll,view,Android,Scroll,View,我有一个包含一些不同布局的长视图。更改设备方向或从其他活动返回后,保持滚动位置的最佳方法是什么?如果我们使用ListView或RecyclerView,它是开箱即用的,但正如我所说的,我只有一个视图。你说只有一个视图是什么意思?滚动视图 如果是这样,我建议您使用片段而不是活动,并在切换时将它们添加到backbackback中。只需从其他布局中放置一个滚动视图作为父视图 这里有一个我的一个项目的例子,我在Scrollview中有一个recyclerview和一个ConstraintLayout &

我有一个包含一些不同布局的长视图。更改设备方向或从其他活动返回后,保持滚动位置的最佳方法是什么?如果我们使用ListView或RecyclerView,它是开箱即用的,但正如我所说的,我只有一个视图。

你说只有一个视图是什么意思?滚动视图


如果是这样,我建议您使用片段而不是活动,并在切换时将它们添加到backbackback中。

只需从其他布局中放置一个滚动视图作为父视图

这里有一个我的一个项目的例子,我在Scrollview中有一个recyclerview和一个ConstraintLayout

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


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