【问题标题】:BrowseFragment next focus issuesBrowseFragment 下一个焦点问题
【发布时间】:2016-07-22 16:30:10
【问题描述】:

我正在使用 Android Leanback API,但我对 BrowseFragment 感到有些头疼。无论出于何种原因,我似乎都无法将焦点从 BrowseFragment 移到其正上方的 ImageButton 上。由于这应该是在电视上,因此用户只能通过方向键移动焦点来导航,没有仅点击按钮的选项。

我在我的一个片段中有以下布局,它基本上创建了一个包含标题的顶行、我想要关注的按钮和一个徽标图像,然后是它下面的 BrowseFragment(我在运行时将其换成FrameLayout 是)。

<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="match_parent"
          android:background="@android:color/transparent"
          android:orientation="vertical"
          android:paddingLeft="3dp"
          android:paddingRight="3dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginLeft="@dimen/lb_browse_padding_start"
    android:layout_marginTop="@dimen/lb_browse_padding_top"
    android:layout_marginRight="@dimen/lb_browse_padding_end"
    >

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/lb_browse_title_text_size"
    android:layout_gravity="left"
    android:layout_centerVertical="true"
    android:id="@+id/category_title"/>

    <ImageView
        android:id="@+id/imageLogo"
        android:layout_width="108dp"
        android:layout_height="44dp"
        android:layout_gravity="right"
        android:layout_alignParentRight="true"
        android:src="@drawable/cc_logo_focused"
        android:layout_centerVertical="true"/>

    <ImageButton
        android:layout_width="@dimen/cc_genre_theme_search_image_width"
        android:layout_height="@dimen/cc_genre_theme_search_image_height"
        android:src="@drawable/search_icon_focus"
        android:layout_gravity="right"
        android:background="@drawable/button_color"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/category_title"
        android:contentDescription="Search button"
        android:scaleType="fitCenter"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:adjustViewBounds="true"
        android:padding="5dp"
        android:clickable="true"
        android:layout_centerVertical="true"
        android:id="@+id/gt_search_button"
        android:backgroundTintMode="add"
        android:backgroundTint="@color/colorTranslucentBackground"/>


</RelativeLayout>

<FrameLayout
    android:id="@+id/CategoryFragment"
    android:name="android.support.v17.leanback.BrowseFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

关于如何让应用正确聚焦的任何想法?谢谢!

【问题讨论】:

    标签: android focus leanback


    【解决方案1】:

    我最终结合使用以下两个函数来解决此问题。在这里发布以防其他人将来遇到类似问题。

    browseFragment 是这些方法所属的 Fragment 类中的一个实例变量。

    private final int maxHookIntoFocusTries = 5;
    private int hookIntoFocusTries = 0;
    
    private void initFocusManagement() {
        View view = browseFragment.getView();
        Handler handler = new Handler();
        if(view == null){
            //Wait for the view to be added
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    initFocusManagement();
                }
            };
            handler.postDelayed(runnable, 250);
        }
        if ( view instanceof ViewGroup) {
            boolean found = hookIntoFocusSearch((ViewGroup) view);
            if ( found ){
                Timber.d("Successfully fixed focus");   //This is just a log
            }else if(hookIntoFocusTries < maxHookIntoFocusTries){
                //Allow multiple attempts to hook into the focus system
                //I want to say this was needed for when the browse fragment was
                //created but the child content hadn't been populated yet.
                //Been a while since I've messed with this code though
                hookIntoFocusTries++;
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initFocusManagement();
                    }
                }, 250);
            }
        }
    
    }
    
    private boolean hookIntoFocusSearch(ViewGroup vg) {
        boolean found = false;
        for ( int i=0; i<vg.getChildCount(); i++ ) {
            View view = vg.getChildAt(i);
            if ( view instanceof BrowseFrameLayout) {
                BrowseFrameLayout bfl = (BrowseFrameLayout)view;
                bfl.setOnFocusSearchListener(new BrowseFrameLayout.OnFocusSearchListener() {
                    @Override
                    public View onFocusSearch(View focused, int direction) {
                        if ( direction == View.FOCUS_UP ) {
                            return searchButton;
                        } else {
                            return null;
                        }
                    }
                });
                found = true;
                break;
            } else if ( view instanceof ViewGroup ) {
                boolean foundInRecurse = hookIntoFocusSearch((ViewGroup)view);
                if ( foundInRecurse ) {
                    found = true;
                    break;
                }
            }
        }
        return found;
    }
    

    编辑:更新代码以包含重试处理程序

    【讨论】:

    • 你在何时何地打电话给这些你能解释一下
    • @Ammarali 自从我弄乱它以来已经有一段时间了,但是查看代码看起来我需要随时从包含 BrowseFragment 的片段中调用它:A) BrowseFragment 已经被初始化并且我们正在恢复片段 B)初始化浏览片段后(我将编辑上面的代码以包含一个重试处理程序,以便我们仍在等待设置视图。可能不是最佳的,但它可以工作)
    【解决方案2】:

    我也遇到过和你一样的问题,我对你的代码进行了一些调整。

    在我扩展的 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 code
        */
    }
    

    这样就不需要使用处理程序和计时器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 2016-01-04
      相关资源
      最近更新 更多