2017-09-04 91 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); 
    } 
}