【问题标题】:Leanback focus problems后倾焦点问题
【发布时间】:2018-01-19 22:37:09
【问题描述】:

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

  RelativeLayout:

   CompoundView: 

   CompoundView:
          RelativeLayout:
            Button
            Button
            RecyclerView

   BrowseFragment:
          Only rows

我的问题是当我到达浏览片段的第一行和其中的第一个项目并且我想向上 (D-PAD-UP) 聚焦按钮时它什么也不做它只在我向左推(D-PAD-LEFT)。任何人都有解决方案?

【问题讨论】:

  • 您应该提供布局 xml 文件的内容,因为您使用的是 RelativeLayout。这将为我们提供组件顺序的线索。
  • @dan 我更新了结构我不使用任何东西,除了 BrowseFragment 中的行
  • RelativeLayout 提供如下属性:android:layout_alignParentTopandroid:layout_toRightOf 等。有关完整列表,请参阅 here。这就是为什么查看使用的对齐方式很重要的原因。
  • 你是对的,RelativeLayout 是造成问题的原因。当我在按钮上设置 centerInParent 时,它没有得到任何焦点。有没有办法避免这种情况?

标签: android android-tv leanback


【解决方案1】:

所以问题出在 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>
    

    这里datesSpinnernameEditText的下方和timesSpinner的左侧,times组件在name的下方和done@98756的下方@ 低于times。见下图:

    请参阅Build Basic TV Layouts 指南了解更多与电视相关的详细信息。

    【讨论】:

    • 嗨,我在使用 RowsSupportFragment 时遇到了类似的问题,这里行的标题而不是行项获得焦点
    • @SubhasmithThapa 您应该使用您的代码详细信息打开一个单独的新问题,这样您就会得到正确的答案。
    • 我已经打开了一个单独的问题,但只是想试试运气。我用这个答案解决了我的问题。请检查。希望对你有帮助stackoverflow.com/questions/68183099/…
    猜你喜欢
    • 2011-03-07
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多