首页 > 解决方案 > 在嵌套的 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>

标签: androidandroid-recyclerviewfocus

解决方案


LinearSnapHelperAFAIK 无法从 XML 布局中做到这一点,但是您可以查看RecyclerView. 如果该特定版本不符合您的需求,您可以通过扩展SnapHelper类来创建自己的版本。


推荐阅读