2017-09-04 5 views
2

私はTVプラットフォーム用のアプリケーションで作業しており、ナビゲーションにはRCUを使用しています。フラグメントのフォーカスを無効にする

私は、2つのフラグメントが互いに上にあり、同時に画面に表示される使用例があります。

以下のフォーカシングフラグメントを無効にする方法はありますか? フラグメントビューのsetFocusable(false)は機能しません。以下のフラグメントに要素をフォーカスすることができます。

ありがとうございます。

+0

setonclicklistnerをonCreateにプログラムで追加できます。 –

+0

このようなものです。 https://stackoverflow.com/a/25841415/3364266 –

+0

なぜonClickListenerですか? onFocusChangedのようなものが必要ですか? 私はタッチイベントを使用しないでください、それはリモコン付きのAndroid TVです。 – Veljko

答えて

2

私は最後に作ってみた解決策は以下のとおりです。すなわち、フラグメントのための

追加カスタムライフサイクルのリスナーonFragmentResumeと私は表示するために必要があるとき、私は手動で呼び出すonFragmentPauseイベントフラグメントを非表示/非表示にする。

@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); 
    } 
} 
関連する問題