【问题标题】:Android TV: Recycler View is not scrollingAndroid TV:回收站视图不滚动
【发布时间】:2021-01-10 08:07:33
【问题描述】:

我在 Android TV 上使用 Recycler View 显示了一个项目列表。但我的列表没有滚动,而是专注于它。请帮我解决这个问题。

private void initRecyclerView(){
    ArrayList<String> categoryList = (ArrayList<String>) getIntent().getSerializableExtra("categoryList"); 
    RecyclerView recyclerView=(RecyclerView) findViewById(R.id.categoryRecyclerView);
    RecyclerViewAdapter adapter=new RecyclerViewAdapter(this,categoryList);
    System.out.println("investmentRecyclerView array: " + categoryList);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

【问题讨论】:

  • 你能发布你的 XML 布局吗?

标签: android android-tv ott


【解决方案1】:

项目的布局设置focusable=true

例如。 list_item.xml 是 RecycerlView 的项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/item_selector"
    android:focusable="true">
    <TextView
        android:id="@+id/topic"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:textColor="#ffffff"
        android:gravity="center"/>
</LinearLayout>

要查看其是否聚焦,将此扇区添加为可绘制对象,并相应地添加图像:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/black" android:state_pressed="true"/>
    <item android:drawable="@color/black" android:state_selected="true"/>
    <item android:drawable="@color/black" android:state_focused="true"/>

    <item android:drawable="@color/lightblue"></item>
</selector>

【讨论】:

  • 当我给的时候,android:background="@drawable/item_selector" 它会被拉伸。设计问题。
  • 然后使用 9 补丁
【解决方案2】:

感谢您的回复。这是我的 XML 代码。

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:id="@+id/parent_layout"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/countries_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="40dp"
        android:textSize="17sp"
        android:text="Canada" />
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/recyclerView"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:focusable="true"
       android:scrollbars="vertical"></androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>

【讨论】:

    【解决方案3】:

    您必须使用 Leanback SDK 中的 VerticalGridViewHorizo​​ntalGridView 而不是 RecyclerView

    <androidx.leanback.widget.HorizontalGridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:lb="http://schemas.android.com/apk/res-auto"
    android:id="@+id/row_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    lb:rowHeight="wrap_content"
    style="?attr/rowHorizontalGridStyle" />
    

    【讨论】:

    • 它可以滚动但不能聚焦?
    • 我认为你做错了什么,或者它可能有焦点但你看不到。为每个项目设置一个背景选择器。