android - 在嵌套的 RecyclerView 中居中焦点项目
问题描述
我正在创建一个 Android 应用程序,用于在我的 Android 电视和手机上查看我个人存储的媒体,我正在尝试获得类似于 Netflix 应用程序的布局,并且 UI 大部分完成,但是当我使用 D'Pad 导航应用程序时控制具有焦点的项目不垂直居中。有没有办法通过布局文件强制这种行为?
活动主
<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>
流派单位
<?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>
流派内部单元
<?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>
解决方案
LinearSnapHelper
AFAIK 无法从 XML 布局中做到这一点,但是您可以查看RecyclerView
. 如果该特定版本不符合您的需求,您可以通过扩展SnapHelper
类来创建自己的版本。
推荐阅读
- ruby - 如何在Ruby中将具有整数值的浮点数转换为整数?
- next.js - 为什么 nextJS 导出页面中的某些文件如此巨大(> 26MB)?
- pine-script - 未知标识符错误,我的策略不会添加到图表中,因为 pine 说它需要输出函数
- ios - dyld:找不到符号:_$s7SwiftUI12ViewModifierP14_viewListCount6inputs4bodySiSgAA01_cfG6InputsV_AgIXEtFZTq
- typescript - 无法让 ts-node 编译带有可选链接的代码
- javascript - Discord Bot 无法断开与 discord.js 中的语音通道的连接
- c# - 使用 C# 表单在 SQL Server 2014 中插入值时出现错误
- java - 使用并行流的forkjoinpool后,futureTask后的代码遇到多线程问题
- javascript - 触发击键以从浏览器在浏览器外部(如在记事本上)键入
- php - 使用“CGI(每个主机可能设置为不同的 PHP 版本)”导致 gettext/_() 崩溃 apache