2017-09-04 90 views
2

我正在为电视平台申请并使用RCU进行导航。禁用焦点片段

我有使用案例,我有两个碎片一个在另一个之上,同时在屏幕上可见。

有没有办法禁用聚焦片段? 片段视图setFocusable(false)不起作用,我可以将元素集中在下面的片段中。

在此先感谢。

+0

您可以以编程方式在onCreate中添加setonclicklistner。 –

+0

这样的事情。 https://stackoverflow.com/a/25841415/3364266 –

+0

为什么onClickListener?我需要像onFocusChanged这样的东西? 我不使用触摸事件,它是与遥控器的Android电视。 – Veljko

回答

2

,我已经在最后想出解决的办法是:

新增定制的生命周期听众为即片段:onFragmentResumeonFragmentPause事件,我手动调用,当我需要证明/隐藏或切换片段。

@Override 
public void onFragmentResume() { 

    //Enable focus 
    if (getView() != null) { 

     //Enable focus 
     setEnableView((ViewGroup) view, true); 

     //Clear focusable elements 
     focusableViews.clear(); 
    } 

    //Restore previous focus 
    if (previousFocus != null) { 
     previousFocus.requestFocus(); 
    } 
} 

@Override 
public void onFragmentPause() { 

    //Disable focus and store previously focused 
    if (getView() != null) { 

     //Store last focused element 
     previousFocus = getView().findFocus(); 

     //Clear current focus 
     getView().clearFocus(); 

     //Disable focus 
     setEnableView((ViewGroup) view, false); 
    } 
} 

/** 
* Find focusable elements in view hierarchy 
* 
* @param viewGroup view 
*/ 
private void findFocusableViews(ViewGroup viewGroup) { 

    int childCount = viewGroup.getChildCount(); 
    for (int i = 0; i < childCount; i++) { 
     View view = viewGroup.getChildAt(i); 
     if (view.isFocusable()) { 
      if (!focusableViews.contains(view)) { 
       focusableViews.add(view); 
      } 
     } 
     if (view instanceof ViewGroup) { 
      findFocusableViews((ViewGroup) view); 
     } 
    } 
} 

/** 
* Enable view 
* 
* @param viewGroup 
* @param isEnabled 
*/ 
private void setEnableView(ViewGroup viewGroup, boolean isEnabled) { 

    //Find focusable elements 
    findFocusableViews(viewGroup); 

    for (View view : focusableViews) { 
     view.setEnabled(isEnabled); 
     view.setFocusable(isEnabled); 
    } 
}