问题描述
我在 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));
}
解决方法
为项目的布局设置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>
,
感谢回复。这是我的 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>
,
您必须使用 Leanback SDK 中的 VerticalGridView 和 HorizontalGridView 而不是 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" />