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



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

你的意思是你只有一个视图?滚动视图?

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

只需将 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>
<?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>

相关内容

最新更新