Android View Framework Summary (ii) View focus

Source: Internet
Author: User

Please respect the results of the sharing, reproduced please indicate the source:
http://blog.csdn.net/hejjunlin/article/details/52263256

Preface: The view frame written to the sixth article, found that the second article did not, and then the matter is in the public number, forget the update on the blog, so the attention of the public should have seen, while today there is time to fill up. (PS: The source code in this article is Android 6.0, please know)

Originally said view next article is written Onmeasure,onlayou,ondraw related, the author do box development, remote control keys, encountered are focus control related. So first focus on the Onmeasure,onlayou,ondraw. Before stepping on a lot of pits, the recent company colleagues to do share, so determined to summarize, agenda as follows:

    • Viewroot
    • The focus of the view
    • The focus of ViewGroup
    • Handling of parent Container focus
    • Lose focus or clear focus
    • Focus move
    • Focusfinder Find Focus
    • Summarize
Android View Focus

Most of the Android focus-related logic is in the view, ViewGroup, and Focusfinder three classes.

Viewroot

The View object has a mparent variable (added to ViewGroup), which refers to its parent container. Most view mparent are viewgroup types, except for the root node. A window in the View root node Decorview mparent called Viewroot, after Android 4.0 viewroot corresponding to Viewrootimpl, it is not a subclass of the view, but a viewparent. Viewrootimpl is the link between the window and the Decorview, the focus of the view, the keys, the layout, the rendering process are all starting from the Viewroot.

The focus of the view

The basic flow is as follows

The View (including ViewGroup) gets the focus through the following three methods:
View.java-Requestfocus ()

From above you can see that the first two will eventually execute to the third method.
The final Requestfocusnosearch first to determine if the focus can be obtained and then enter the following process:
View.java-Requestfocusnosearch ()

View.java-Handlefocusgaininternal ()

The process above is simple: if there is currently no focus, set the focus flag, then notify parent, and then refresh the picture.

The main process is in the requestchildfocus of Mparent, which is analyzed later. It will change the focus view and clear the focus of the original focus view.
Onfocuschange triggers a invalidate refresh and then calls Onfocuschangelistener. By default, only one onfocuschangelistener per view can be set, and in development it is often necessary to set up multiple listener, and we may override the Onfocuschange method. Implement the need to callback multiple Onfocuschangelistener.

The focus of ViewGroup

ViewGroup get focus is more internal focus processing in the view capture focus process
Viewgroup.java-Requestfocus ()

In the above code, Descendantfocusability determines whether to first handle the view focus process (own focus) or to give the child view processing

    • Focus_block_descendants does not allow the sub-view to get the focus, then follow the view process
    • Focus_before_descendants is handled by the view process first, and if you can't get the focus, treat your child
    • Focus_after_descendants first try to give the child view focus, if there is no focusable and then follow the view process to get focus
    • Focus_before_descendants, the default value we can set by setdescendantfocusability (int d)

The Onrequestfocusindescendants method is used to override the subclass, which controls the child view processing focus. Default is processed in sub-view order, direction down or to the right from the first, up or to the left, starting from the last, until a child view gets focus

(Note that this method will only be executed when the ViewGroup and its upper view are raised with Requestfocus)

Handling of parent Container focus

Mparent.requestchildfocus is called during the View capture focus process to maintain the focus on the view tree and to preserve the focused child view in each layer ViewGroup

Viewgroup.java-Requestchildfocus ()

Clear your focus first, clear its focus, save the child that gets the focus, and then call the Requestchildfocus on the previous layer if there is a focus inside the original. The final call shows that this method will always be called to the root node of the view tree.

Within the current viewgroup, any child's focus will be carried out to this method, so this method is one of the ways ViewGroup learns that the child's focus changes. (It's too bad to know that the child loses focus)

Lose focus or clear focus

Getting focus can be active, but losing focus is generally passive (see the code above), so logic is relatively simple, just clear the focus state.

Viewgroup.java-Unfocus ()

View.java, Unfocus (), clearfocusinternal ()

Note that the method above is the default package access level, and we cannot override or call

You can also proactively clear the focus, similar to the get focus process
View.java-Clearfocus ()

Viewgroup.java-Clearfocus ()

Viewgroup.java-Clearchildfocus ()

These are all the processes and methods involved in the focus processing of the Android view system, Viewrootimpl Requestchildfocus and Clearchildfocus implementations we don't need to focus on

In addition, there are some of the following auxiliary methods

    • Boolean isfocusable () view can get focus
    • Boolean isFocused () View gets focus
    • Boolean hasfocus () View/viewgroup internal whether there is focus
    • View Findfocus () takes the focus view inside the View/viewgroup
    • View Getfocusedchild () takes the child view with focus inside the ViewGroup
    • View Getrootview () takes the root node view (typically Decorview or top level ViewGroup)
Focus move

In addition to the control of the focus in the code, the system does not handle the direction keys and other keys automatically follow the focus movement to deal with, see the following code

Viewrootimpl.java-Processkeyevent ()



Code comparison, but there are three main steps

    1. If the view does not handle the key, the upper and lower left and right tab keys are converted to the corresponding direction
    2. Finds the next view in the corresponding direction using the Focussearch method on the current focus view
    3. Lookup to the view call Requestfocus, so the main process is in Focussearch

View.java-Focussearch ()

Normal view find nothing to do, give it to the parent to complete.

Viewgroup.java-Focussearch ()

Viewrootimpl.java-Focussearch ()

We can override the Focussearch control focus move order, and the default focus move order is determined by Focusfinder

Focusfinder Find Focus

Focusfinder is the public tool class, mainly two methods, can be in a given view in the specified direction to find the next focus of the specified view or coordinates
As follows:
Focusfinder.java-Findnextfocus ()

The core logic is two-step, first find Setnextfocusxxid set of view, if not according to the nearest algorithm to find.
The specific algorithm is no longer analyzed, the SDK has source code.

Summarize

Combining the above process analysis, we have the following ideas about the special needs of the focus when implementing a custom view

    • Requestfocus and Clearfocus directly to the view to clear or shift focus
    • In addition to Onfocuschangelistener, you can also implement some notifications when the view loses/receives focus in the Onfocuschange method
    • For ViewGroup, if you only need to be notified when the child view gets focus, there is a Requestchildfocus method.
    • Override the Onrequestfocusindescendants method to control ViewGroup focus in some scenarios
    • Control focus move can override the Focussearch method
    • The process of finding focus, mainly from the view of the Focussearch (...) The method begins with the current focus, starting from the layer to the outside, and finally executing the core method of Focusfinder in the outermost layout to get the views view of the next focus.
    • There are also focusfinder tools and the above auxiliary methods.
    • Before the view has the focus, you must first determine whether the view has access to the focus, which can be judged by isfocusable and Isfocusableintouchmode, or the preceding code.
    • You can also set the permissions for the specified view to have the focus by using setfocusable and Setfocusableintouchmode.
    • The attribute that gets the focus in the XML blocking Child view is: android:descendantfocusability = "Blocksdescendants", However, if the current activity after pause or stop and then re-resume after the property will be invalidated, then you can add the Requestfocusfromtouch method in the Onresume will be able to re-enter the property.

The first time to get blog update reminders, as well as more Android dry, source code Analysis , Welcome to follow my public number, sweep the bottom QR code or long press to identify two-dimensional code, you can pay attention to.

If you feel good, easy to praise, but also to the author's affirmation, can also share this public number to you more people, original not easy

Android View Framework Summary (ii) View focus

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.