我正在尝试创建一个应用程序,它使用 DrawerLayout 来显示几个选项,这些选项用于影响用户呈现的结果。这是显示当前结果的模拟器捕获:
现在,我还使用了一个工具栏,其唯一目的是在屏幕左上方显示打开菜单按钮:
为了实现这一点,我遵循了官方文档中描述的步骤。这是我的主要活动的 XML:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_below="@id/toolbar"
android:fitsSystemWindows="true">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="#FFE7E8ED">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:titleTextColor="@color/colorWhite" />
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swiper"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/lista"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="true"></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_viewer" />
</android.support.v4.widget.DrawerLayout>
我添加了一个 SwipeRefreshLayout 来让用户刷新 RecyclerView 中显示的数据。在这种情况下,我有嵌套的 RecyclerViews,因为我需要显示的数据按类别分组:
category A
news 1
news 2
category B
news 3
....
现在,我的问题是,当应用程序加载时,它会“跳转”到包含新闻列表的卡片视图。这张图片显示了我面临的问题:
红色框显示类别项的开始位置(只是一些纯文本)。为了说明我的观点,这是布局工具的另一个屏幕截图:
如您所见,标题在那里,但它在工具栏后面向上滚动。就好像内部列表将自己提升到屏幕顶部......
为了彻底起见,我还展示了我在顶部 RecyclerView 的项目模板中使用的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/catalog_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="20dp"
android:textAlignment="inherit"
android:textAppearance="@android:style/TextAppearance.Material.WindowTitle"
android:textColor="@android:color/darker_gray"
android:textSize="16sp"
android:textStyle="bold" />
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:cardElevation="4dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/lista_noticias"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:clipToPadding="false">
</android.support.v7.widget.RecyclerView>
</android.support.v7.widget.CardView>
</LinearLayout>
这是我用于内部 RecyclerView 的项目模板的模板
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="140dp"
android:maxHeight="140dp"
android:minHeight="140dp"
card_view:cardCornerRadius="0dp"
card_view:contentPadding="2dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/logo"
android:layout_width="73dp"
android:layout_height="73dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="false"
android:scaleType="fitXY"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent"
card_view:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="1"
android:textAppearance="@android:style/TextAppearance.Material.Title"
android:textSize="16sp"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toEndOf="@+id/logo"
card_view:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/description"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/title"
android:layout_marginBottom="32dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ellipsize="end"
android:gravity="top|center_vertical"
android:lines="3"
android:text="Name"
android:textAppearance="@android:style/TextAppearance.Material.SearchResult.Subtitle"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toEndOf="@+id/logo"
card_view:layout_constraintTop_toBottomOf="@+id/title"
card_view:layout_constraintVertical_chainStyle="spread_inside" />
<TextView
android:id="@+id/publishDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:text="10/10/2018 13:50"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textSize="10sp"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
正如我所说,就好像新闻列表滚动 UI 以显示第一项。我还注意到,如果没有足够的项目来获得第一组的卷轴,那么一切都会很好。这是一张图片,显示:
但是,一旦我选择了一个新闻比可用屏幕空间多的类别,它就会简单地将视图定位在内部列表的第一项上。
有人可以告诉我我错过了什么吗?
多谢你们!
路易斯