【问题标题】:Customize BrowseFragment to show text and image preview above Row list自定义 BrowseFragment 以在行列表上方显示文本和图像预览
【发布时间】:2019-06-20 17:11:17
【问题描述】:

由于我们是 Android TV 开发的新手,我们愿意设计如下屏幕截图所示的屏幕。

在对 Leanback 库进行了某种研究之后,才知道我们应该使用 BrowseFragment 来达到预期的效果。

现在的问题是如何设计顶部的预览部分,结合文本和程序海报,因为 BrowseFragment 从屏幕左上角显示行。

我们是否需要使用我们的自定义,或者我们可以使用任何 Leanback 库 Fragment/Class/Activity?See screenshot here

【问题讨论】:

    标签: android android-tv leanback


    【解决方案1】:

    我建议将 BrowseFragment 作为子片段包装到您的片段中,其中包含您想要的任何信息(TextView、ImageView 或其他)。 BrowseFrament 的 UI 行为很难改变,因为大部分代码/资源都是私有的或受保护的。

    我假设您有一个 RootFragment,其中包含两个子片段,即 HeaderFragment 和 ContentFragment。

    根片段

    <androidx.constraintlayout.widget.ConstraintLayout
        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="match_parent"
        android:paddingTop="0dp"
        android:paddingBottom="0dp"
        android:paddingLeft="0dp"
        android:paddingRight="0dp">
      <fragment
            android:id="@+id/headersView"
            android:name="yourpackagename.HeadersFragment"
            android:layout_width="120dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
    <fragment
            android:id="@+id/contentFragment"
            android:name="yourpackagename.ContentFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/headersView"
            app:layout_constraintTop_toTopOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    HeaderFragment的布局

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.leanback.widget.VerticalGridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:lb="http://schemas.android.com/tools"
        android:id="@+id/headersView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        lb:numberOfRow="1" />
    

    对于 ContentFragment,您只需扩展 androidx.leanback.app.RowsSupportFragment() 即可加载数据。因此,您可以相应地更改您喜欢的任何 UI。

    【讨论】:

    • 嗨 Jason,您能否更详细地解释您的答案如何将 BrowseFragment 包装为子片段?或者举个例子来帮助我们理解。
    • 嘿 Shekhar,对不起,我犯了一些错误。我们不使用 BrowseFragment 是因为定制非常困难。相反,我们使用自定义 Fragment 来实现类似 BrowseFragment 的行为。您需要一个包含两个子片段(左和右)的片段。对于左侧的片段,我们使用“androidx.leanback.widget.VerticalGridView”来显示导航栏。对于正确的片段,我们扩展了“androidx.leanback.app.RowsSupportFragment”以显示多行和图块。这样做,您可以轻松更改 UI。
    • 嗨,Jason,感谢您的解释,但我们是 Android TV 开发的新手,所以如果您只为我们提供一个基本示例或相同的代码 sn-p,将会非常有帮助。
    • 我编辑了我的答案以包含一些 xml,但是对于如何使用 VerticalGridView 和 RowsSupportFrament,您需要用谷歌搜索它。 Leanback 会自动为您处理焦点。
    • 感谢 Jason 的示例,我会尝试并回复您。
    猜你喜欢
    • 1970-01-01
    • 2021-04-29
    • 2019-07-27
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多