禁用焦点片段

时间:2017-09-04 09:45:49

标签: android fragment focus

我正在申请电视平台并使用RCU进行导航。

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

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

提前致谢。

3 个答案:

答案 0 :(得分:4)

我最终提出的解决方案是:

为片段添加自定义生命周期监听器,即: 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);
    }
}

答案 1 :(得分:0)

我解决了这个问题,为片段布局的父ViewGroup创建了一个自定义类。在自定义ViewGroup中,您可以覆盖方法

focusSearch(focused: View?, direction: Int)

添加此逻辑:

override fun focusSearch(focused: View?, direction: Int): View? {

        val focusSearch = super.focusSearch(focused, direction)

        if (direction == View.FOCUS_DOWN) when (focused?.id) {
            R.id.some_view_that_has_focus -> return new_view_focus
        }

        if (findViewById<View>(focusSearch.id) == null) {//the view found is not part of this parent return null
            return null
        }

        return focusSearch
}

如果新的焦点视图不属于此parent,则返回null。如果无法达到某些其他“视图”焦点,则可以在when情况下手动进行管理。

答案 2 :(得分:0)

我遇到了同样的问题,被接受的答案对我有用。

这是我到目前为止的实现版本(可以改进):

abstract class BaseFragment<....> : Fragment() {

    private val screenFocusHelper = ScreenFocusHelper()

    fun enableFocus() {
        if (view != null) {
            // Enable focus
            screenFocusHelper.setEnableView(view as ViewGroup, true)

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

        childFragmentManager.fragments.forEach {
            if (it is BaseFragment<*, *>) {
                it.enableFocus()
            }
        }
    }

    fun disableFocus() {
        if (view != null) {
            // Store last focused element
            screenFocusHelper.previousFocus = view?.findFocus()

            // Clear current focus
            view!!.clearFocus()

            // Disable focus
            screenFocusHelper.setEnableView(view as ViewGroup, false)
        }

        childFragmentManager.fragments.forEach {
            if (it is BaseFragment<*, *>) {
                it.disableFocus()
            }
        }
    }

}

class ScreenFocusHelper {

    var previousFocus: View? = null

    val focusableViews: MutableList<View> = mutableListOf()

    fun setEnableView(viewGroup: ViewGroup, isEnabled: Boolean) {
        findFocusableViews(viewGroup)

        for (view in focusableViews) {
            view.isEnabled = isEnabled
            view.isFocusable = isEnabled
        }
    }

    private fun findFocusableViews(viewGroup: ViewGroup) {
        val childCount = viewGroup.childCount
        for (i in 0 until childCount) {
            val view = viewGroup.getChildAt(i)
            if (view.isFocusable) {
                if (!focusableViews.contains(view)) {
                    focusableViews += view
                }
            }
            if (view is ViewGroup) {
                findFocusableViews(view)
            }
        }
    }

}