自定义ViewGroup焦点处理 [英] Custom ViewGroup focus handling

查看:393
本文介绍了自定义ViewGroup焦点处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个自定义的ViewGroup,这个ViewGroup是可调焦的,并且有一些子视图也是可以调焦的(一个自定义的Android机顶盒的垂直菜单,它应该在遥控器上做出反应)。



每当自定义ViewGroup获得焦点时,我需要将焦点传递给一些子视图。



我将 descendantFocusability 设置为 beforeDescendants 并将 OnFocusChangeListener ViewGroup但永远不会调用 OnFocusChangeListener.onFocusChanged()。它看起来像 beforeDescendants 不能按我的预期工作。实际上,设置 beforeDescendants 与设置 afterDescendants 相同 - 焦点由最近的子视图和自定义ViewGroup没有机会决定哪个子视图需要关注。



我该如何达到所需的行为?什么是处理焦点在 ViewGroup s?

解决方案

在Android资源挖掘后,我知道如何派遣焦点到 ViewGroup 的孩子。例如,我在里面定制了 ViewGroup ,里面没有 EditText 字段。当用户点击 ViewGroup (每个 EditText 之外)时,我想把焦点调度到一个字段。 p>

为此,我们需要将descendantFocusability设置为 FOCUS_AFTER_DESCENDANTS 。这意味着我们可以在之前处理焦点事件,我们的自定义视图将尝试着重于自己:

  setDescendantFocusability (FOCUS_AFTER_DESCENDANTS); 



下一步是重写 onRequestFocusInDescendants 方法,它将在
自定义视图的 requestFocus 之前被调用 FOCUS_AFTER_DESCENDANTS 被设置。在这个方法中,我们可以请求关注子对象:

$ @ $ $
保护布尔onRequestFocusInDescendants(final int dir,final Rect rect ){
return getChildAt(this.target).requestFocus();



$ b $ p
$ b

这个方法的一个重要的事情是:如果我们返回真正的在这里,我们的自定义视图将不会被要求重点(只有 FOCUS_AFTER_DESCENDANTS )。



通过这种方式,当点击 ViewGroup 时,我们可以改变 target 字段来改变哪个孩子会被关注。

$ h
为了更好地理解,可以在 requestFocus 方法> ViewGroup 在Android来源。


Let's say I have a custom ViewGroup which is focusable and has some child views which are focusable as well (A custom vertical menu for Android set-top-boxes which should react on remote controller).

I need to pass a focus to some of the child views whenever the custom ViewGroup gains the focus.

I set descendantFocusability to beforeDescendants and set OnFocusChangeListener to the custom ViewGroup but OnFocusChangeListener.onFocusChanged()is never called. It looks like beforeDescendants does not work as I expected. Actually setting the beforeDescendants works the same as setting the afterDescendants - The focus is taken by the nearest child view and the custom ViewGroup does not have an opportunity to decide which child view should take the focus.

How can I achieve the desired behavior? What are the best practices to handling focuses in ViewGroups?

解决方案

After digging in Android sources I understand how to dispatch focus to ViewGroup's children. For example I have custom ViewGroup with few EditText fields inside. When the user clicks on ViewGroup (outside of each EditText) I want to dispatch focus to one of fields.

To do this we need to set descendantFocusability to FOCUS_AFTER_DESCENDANTS. It means that we can handle focus event before our custom view will try to focus itself:

setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);


Next step is to override onRequestFocusInDescendants method, it will be invoked before custom view's requestFocus if flag FOCUS_AFTER_DESCENDANTS was set. In this method we can request child focus:

@Override
protected boolean onRequestFocusInDescendants(final int dir, final Rect rect) {
    return getChildAt(this.target).requestFocus();
}

One important thing with this method: if we return true here, our custom view will not be requested to focus (only with FOCUS_AFTER_DESCENDANTS).

In such a manner we can change target field to change which child will be focused when ViewGroup is clicked.


For better understanding you can find requestFocus method in ViewGroup in Android sources.

这篇关于自定义ViewGroup焦点处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆