2017-08-11 138 views
0

好家伙我的工作申请和我的布局结构如下:Leanback的焦点问题

RelativeLayout: 

    CompoundView: 

    CompoundView: 
      RelativeLayout: 
      Button 
      Button 
      RecyclerView 

    BrowseFragment: 
      Only rows 

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

+0

由于您使用了'RelativeLayout',因此您应该提供布局xml文件的内容。这将给我们提供组件顺序的线索。 – dan

+0

@dan我更新的结构我不使用除浏览片段内的行之外的任何东西 – user3820641

+0

'RelativeLayout'提供的属性如:'android:layout_alignParentTop'或'android:layout_toRightOf'等等。请参阅[这里](https://developer.android .com/guide/topics/ui/layout/relative.html)获取完整列表。这就是为什么重要的是要看到什么是使用的配合。 – dan

回答

0

所以这个问题出于某种原因在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 
    */ 
} 

瞧它的工作原理。

1

由于您使用的是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之一,并且doneButton低于times。见下图:

enter image description here

Build Basic TV Layouts引导更多电视相关细节。