1、
requestLayout:当view确定自身已经不再适合现有的区域时,该view本身调用这个方法要求parent view重新调用他的onMeasure onLayout来对重新设置自己位置。
特别的当view的layoutparameter发生改变,并且它的值还没能应用到view上,这时候适合调用这个方法。
invalidate:View本身调用迫使view重画。
上面的描述,我觉得有问题:
下面是一个简单地描述:
View.requestLayout() 请求重新布局
View.invalidate() 刷新视图,相当于调用View.onDraw()方法
2、findFocuse()
Find the view in the hierarchy rooted at this view that currently has focus.
看下面的代码:会根据当前focused的控件找到下一个获得focused的控件。
View currentFocused = findFocus(); View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);可以参考一下ScrollView arrowScroll方法的源码
3、requestFocuse
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.
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. |
protected void onFocusChanged( boolean gainFocus, int direction, Rect previouslyFocusedRect )方法,第三个参数就是requestFocus中传进来的。
那么问题来了,如何进行坐标系转换呢?
android的ViewGroup中提供了两个很NB的方法:
Offset a rectangle that is in a descendant's coordinate space into our coordinate space.
descendant | A descendant of this view |
---|---|
rect | A rectangle defined in descendant's coordinate space. |
Offset a rectangle that is in our coordinate space into an ancestor's coordinate space.
descendant | A descendant of this view |
---|---|
rect | A rectangle defined in descendant's coordinate space. |