禁用焦点片段

Vel*_*jko 6 android focus fragment

我正致力于电视平台的应用,并使用RCU进行导航.

我有一个用例,我有两个碎片一个在彼此之上,同时在屏幕上可见.

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

提前致谢.

Vel*_*jko 5

我最后想出的解决方案是:

为片段添加了自定义生命周期侦听器,即:当我需要显示/隐藏或在片段之间切换时手动调用的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);
    }
}
Run Code Online (Sandbox Code Playgroud)