在嵌套的 RecyclerView 中将焦点项目居中
Centering the focused item in nested RecyclerView
我正在创建一个 Android 应用程序用于我的 Android 电视和 phone 以查看我个人存储的媒体,我正在尝试获得类似于 Netflix 的布局应用程序并使 UI 基本完成,但是当我使用 D'Pad 控件导航应用程序时,具有焦点的项目不会垂直居中。有没有办法通过布局文件强制执行此行为?
activity_main
<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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context="activity.MainActivity">
<android.support.constraint.ConstraintLayout
android:id="@+id/scroll_view"
android:layout_width="740dp"
android:layout_height="265dp"
android:layout_gravity="center"
android:layout_marginBottom="50dp"
android:background="#000000"
android:clickable="false"
android:focusable="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/genre_recycler"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#000000"
android:clickable="false"
android:focusable="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
genre_unit
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="@dimen/card_corner_radius"
app:cardElevation="@dimen/card_elevation"
android:clickable="false"
android:focusable="false"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:clickable="false"
android:focusable="false">
<TextView
android:id="@+id/genre_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textColor="#FFFFFF"
android:textAppearance="@style/Genre.Title"
android:clickable="false"
android:focusable="false"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/genre_inner_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/genre_heading"
android:clickable="false"
android:focusable="false"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
genre_inner_unit
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:clickable="false"
android:focusable="false">
<ImageView
android:id="@+id/genre_movie_image"
android:layout_width="70dp"
android:layout_height="115dp"
android:scaleType="fitXY"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"/>
<TextView
android:id="@+id/genre_movie_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/genre_movie_image"
android:layout_alignLeft="@id/genre_movie_image"
android:layout_alignRight="@id/genre_movie_image"
android:text="Movie Title"
android:textColor="#FFFFFF"
android:clickable="false"
android:focusable="false"/>
</RelativeLayout>
据我所知,无法从 XML 布局中做到这一点,但是您可以查看 RecyclerView
附带的 LinearSnapHelper
class。如果那个特定的版本不符合您的需要,您可以通过扩展 SnapHelper
class.
创建自己的版本
我正在创建一个 Android 应用程序用于我的 Android 电视和 phone 以查看我个人存储的媒体,我正在尝试获得类似于 Netflix 的布局应用程序并使 UI 基本完成,但是当我使用 D'Pad 控件导航应用程序时,具有焦点的项目不会垂直居中。有没有办法通过布局文件强制执行此行为?
activity_main
<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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context="activity.MainActivity">
<android.support.constraint.ConstraintLayout
android:id="@+id/scroll_view"
android:layout_width="740dp"
android:layout_height="265dp"
android:layout_gravity="center"
android:layout_marginBottom="50dp"
android:background="#000000"
android:clickable="false"
android:focusable="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/genre_recycler"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#000000"
android:clickable="false"
android:focusable="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
genre_unit
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="@dimen/card_corner_radius"
app:cardElevation="@dimen/card_elevation"
android:clickable="false"
android:focusable="false"
android:gravity="center_vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:clickable="false"
android:focusable="false">
<TextView
android:id="@+id/genre_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textColor="#FFFFFF"
android:textAppearance="@style/Genre.Title"
android:clickable="false"
android:focusable="false"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/genre_inner_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/genre_heading"
android:clickable="false"
android:focusable="false"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
genre_inner_unit
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:clickable="false"
android:focusable="false">
<ImageView
android:id="@+id/genre_movie_image"
android:layout_width="70dp"
android:layout_height="115dp"
android:scaleType="fitXY"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"/>
<TextView
android:id="@+id/genre_movie_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/genre_movie_image"
android:layout_alignLeft="@id/genre_movie_image"
android:layout_alignRight="@id/genre_movie_image"
android:text="Movie Title"
android:textColor="#FFFFFF"
android:clickable="false"
android:focusable="false"/>
</RelativeLayout>
据我所知,无法从 XML 布局中做到这一点,但是您可以查看 RecyclerView
附带的 LinearSnapHelper
class。如果那个特定的版本不符合您的需要,您可以通过扩展 SnapHelper
class.