Android学习之View和ViewGroup

1、

 requestLayout:当view确定自身已经不再适合现有的区域时,该view本身调用这个方法要求parent view重新调用他的onMeasure onLayout来对重新设置自己位置。

特别的当view的layoutparameter发生改变,并且它的值还没能应用到view上,这时候适合调用这个方法。
invalidate:View本身调用迫使view重画。


上面的描述,我觉得有问题:

下面是一个简单地描述:

View.requestLayout() 请求重新布局

View.invalidate() 刷新视图,相当于调用View.onDraw()方法

2、findFocuse()

public View findFocus ()

Added in  API level 1

Find the view in the hierarchy rooted at this view that currently has focus.

Returns
  • The view that currently has focus, or null if no focused view can be found.
google官方解释,实在是不太理解。不过这是一个很重要的方法。

看下面的代码:会根据当前focused的控件找到下一个获得focused的控件。

View currentFocused = findFocus();
View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
可以参考一下ScrollView  arrowScroll方法的源码

3、requestFocuse

public boolean requestFocus (int direction, Rect previouslyFocusedRect)

Added in  API level 1

Call this to try to give focus to a specific view or to one of its descendants and give it hints about the direction and a specific rectangle that the focus is coming from. The rectangle can help give larger views a finer grained hint about where focus is coming from, and therefore, where to show selection, or forward focus change internally. A view will not actually take focus if it is not focusable (isFocusable() returns false), or if it is focusable and it is not focusable in touch mode (isFocusableInTouchMode()) while the device is in touch mode. A View will not take focus if it is not visible. A View will not take focus if one of its parents has getDescendantFocusability() equal toFOCUS_BLOCK_DESCENDANTS. See also focusSearch(int), which is what you call to say that you have focus, and you want your parent to look for the next one. You may wish to override this method if your custom View has an internalView that it wishes to forward the request to.

Parameters
direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
previouslyFocusedRect The rectangle (in this View's coordinate system) to give a finer grained hint about where focus is coming from. May be null if there is no hint.
Returns
  • Whether this view or one of its descendants actually took focus.
注意看previouslyFocusedRect的说明,这个区域坐标要求在调用requestFocuse方法的view坐标系统中,调用requestFocuse方法最终会调用到

protected void onFocusChanged( boolean gainFocus, int direction, Rect previouslyFocusedRect )方法,第三个参数就是requestFocus中传进来的。

那么问题来了,如何进行坐标系转换呢?

android的ViewGroup中提供了两个很NB的方法:

public final void offsetDescendantRectToMyCoords (View descendant, Rect rect)

Added in  API level 1

Offset a rectangle that is in a descendant's coordinate space into our coordinate space.

Parameters
descendant A descendant of this view
rect A rectangle defined in descendant's coordinate space.

public final void offsetRectIntoDescendantCoords (View descendant, Rect rect)

Added in  API level 1

Offset a rectangle that is in our coordinate space into an ancestor's coordinate space.

Parameters
descendant A descendant of this view
rect A rectangle defined in descendant's coordinate space.


你可能感兴趣的:(Android学习之View和ViewGroup)