Leanback焦点问题

3

大家好,我正在开发一个应用程序,我的布局结构如下:

  RelativeLayout:

   CompoundView: 

   CompoundView:
          RelativeLayout:
            Button
            Button
            RecyclerView

   BrowseFragment:
          Only rows

我的问题是,当我到达浏览片段的第一行和其中的第一项时,并且想要向上移动 (D-PAD-UP) 来聚焦按钮时,它没有任何作用,只有当我按左侧 (D-PAD-LEFT) 时才起作用。有没有解决方法?


你应该提供你的布局XML文件的内容,因为你正在使用RelativeLayout。这将给我们一个组件顺序的线索。 - dan
@dan,我更新了结构,除了在BrowseFragment内部使用行之外,我不使用任何其他东西。 - harisk92
RelativeLayout 提供了像 android:layout_alignParentTopandroid:layout_toRightOf 等属性。完整列表请参见此处。这就是为什么查看所使用的对齐方式非常重要的原因。 - dan
你说得对,RelativeLayout引起了问题。当我在按钮上设置centerInParent时,它没有任何焦点。有什么方法可以避免这种情况吗? - harisk92
2个回答

4

所以问题出在BrowseFrameLayout上,我需要覆盖onFocusSearchListener方法并自己管理焦点来解决这个问题。

在我扩展的BrowseFragment中,我有以下方法:

public void workaroundFocus(){
    if(getView() != null) {
        View viewToFocus  = getActivity().findViewById(R.id.view_to_focus);
        BrowseFrameLayout browseFrameLayout = getView().findViewById(android.support.v17.leanback.R.id.browse_frame);
        browseFrameLayout.setOnFocusSearchListener((focused, direction) -> {
            if (direction == View.FOCUS_UP) {
                return viewToFocus;
            }
            else {
                return null;
            }
        });
    }
}

然后:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    workaroundFocus();
    /*
      Rest of the code
    */
}

然后,它就可以工作了。


2

由于您正在使用RelativeLayout,因此应按照希望导航的顺序布置组件。

使用在RelativeLayout参考文档中介绍的XML属性,您也可以建立导航顺序:

<?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"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/reminder" />
    <Spinner
        android:id="@+id/dates"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/times" />
    <Spinner
        android:id="@id/times"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentRight="true" />
    <Button
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/times"
        android:layout_alignParentRight="true"
        android:text="@string/done" />
</RelativeLayout>

这里,日期 Spinner 位于名称 EditText 下方,同时在左侧是时间 Spinner时间 组件在名称 组件下方,完成 Button时间 下方。请参见下图:enter image description here 更多与电视相关的详细信息,请参见构建基本电视布局指南。

嗨,我遇到了一个类似的问题,我正在使用RowsSupportFragment,在这里行的标题获得了焦点,而不是行项目。 - Subhasmith Thapa
@SubhasmithThapa,你应该开一个新的问题,并提供你的代码细节,这样你才能得到一个恰当的答案。 - dan
我已经开了一个单独的问题,但只是想试试运气。我用这个答案解决了我的问题。请检查一下。希望能有所帮助 https://stackoverflow.com/questions/68183099/how-to-focus-on-the-listrow-item-which-is-not-focusable-on-firestick-3rd-generat - Subhasmith Thapa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接