apk development framework! 30 suggestions to help programmers improve their core competitiveness, speak plainly!

beginning

Generally speaking, there are three reasons why middle-aged programmers are easily eliminated.
1. The output capacity has reached its peak. This person has been struggling for more than ten years, and he is still doing nothing. Obviously, this is the ceiling of this person. To put it bluntly, this is the talent.
2. The ability to adapt is getting worse. Older age, family, old fritters, decline in learning ability, stubborn work, etc.
3. The cost performance is gradually decreasing. By the age of 35, the annual salary will not be low, but the output capacity has reached the ceiling. As the age gradually increases, the cost performance will become lower and lower. Let's just talk about the classic sentence: Substitution is too high. 25-year-old young people are cheap and promising, 35-year-old has realized the talent price is still high, when the two choices are similar, how do you think the company will choose...Anyone who becomes a boss will choose the former. And how to increase their irreplaceability?

Interviewer: Tell me about the event distribution in the view?

android android event distribution mechanism is a common problem, we all know that the general view is that the distribution event from the view Viewgroup(Parent)#dispatchTouchEventto Viewgroup(Parent)#onInterceptTouchEventthen View#dispatchTouchEvent, and then to the view onTouchEvent, and finally back to Viewgroup(Parent)#onTouchEvent. If you can’t remember the method name, you can directly say that it is the distribution of the parent first, the interception, then the distribution of the view, the consumption of the view, and the consumption of the parent.

viewgroup distribution

This answer is definitely very simple, because it does not say whether to intercept, whether to distribute, whether to consume various conditions, does not involve the distribution of various actions, the default distribution mentioned above is only for action_down, because of view/viewgroupvarious super calls They are not distributed, intercepted, or consumed, so when the view that handles the touch event is not found, it is always passed to the upper view, and passed to the activity. Let's organize it again:

If viewgroup not be distributed, then action_down, action_moveand action_uponly to viewgroup execution dispatchTouchEvent, the conditions are not distributed dispatchTouchEventdirectly returns true or false, the difference between true and false is true is executed action_down, action_moveand action_up, if direct return false performed only to action_down. And the onInterceptTouchEventsubsequent methods of the subsequent viewgroup will not be executed.

Regarding why dispatchTouchEventall three actions can be executed when view/Viewgroup returns true, but if it returns false, only action_down can be executed. This needs to be answered in the parent class of view/Viewgroup dispatchTouchEvent. This method will be in action_down. Call the dispatchTransformedTouchEventmethod, and the method is to determine the return value of the parent class dispatchTransformedTouchEventmethod through the return value of the dispatchTouchEvent method of the child view , and dispatchTransformedTouchEventthe return value will determine mFirstTouchTargetwhether it is empty, so in the process of action_down, it is actually dispatchTouchEventreturned by the method of the child view Value to determine mFirstTouchTargetwhether it is empty. Post dispatchTransformedTouchEventthe deletion code of the method in the viewgroup here :

private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
        View child, int desiredPointerIdBits) {
    ------------------
    //省略了cancel部分的代码
    ------------------------
    //如果child为空,直接调用自己的dispatchTouchEvent方法,此时自己就相当于一个view,touch事件走自己的
    if (child == null) {
        handled = super.dispatchTouchEvent(transformedEvent);
    } else {
        final float offsetX = mScrollX - child.mLeft;
        final float offsetY = mScrollY - child.mTop;
        transformedEvent.offsetLocation(offsetX, offsetY);
        if (! child.hasIdentityMatrix()) {
            transformedEvent.transform(child.getInverseMatrix());
        }
        //返回值直接通过孩子来获取返回值
        handled = child.dispatchTouchEvent(transformedEvent);
    }
    transformedEvent.recycle();
    return handled;
}

So if the view/viewgroup dispatchTouchEventmethod returns false, it means that the parent class dispatchTransformedTouchEventmethod returns false during action_down ; if it returns true, the addTouchTargetmethod will be called and mFirstTouchTargetset the value:

private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
    final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
    target.next = mFirstTouchTarget;
    mFirstTouchTarget = target;
    return target;
}

Then it will be called again later:

This sentence dispatchTouchEventwill only go here when the view/viewgroup returns false, so the subsequent action_movesum action_upwill go here, and the child=null passed in at this time, as can be seen from the above code, the dispatchTouchEventmethod of the parent class is directly called . So it is not difficult to see from this that when the view/viewgroup dispatchTouchEventreturns false, the dispatchTouchEventmethod of the parent class is directly called , so there is only the action_down event.

Interviewer: If I only want to have the drag and drop event of the view, but not the click event of the view, let you rewrite the design of the drag and drop of the view

In fact, this question examines everyone's processing flow for the dispatchTouchEvent of the view and the onTouchEvent of the view. The above has analyzed the touch event of the view to be executed by the view, then the view must be required to dispatchTouchEventreturn true, and dispatchTouchEventreturn true or dispatchTouchEventreturn true directly Or the view onTouchEventreturns true. From an efficiency point of view, dispatchTouchEventit is ok to return true directly , without the need to care about the onTouchEventmethod.

viewgroup interception

About interception is nothing more than interception or non-interception, and the condition of interception is to return true, not to intercept is to return false or return to super.onInterceptTouchEvent, the default super returns false, so you can use super to mean no interception

Viewgroup interception is actually by dispatchTouchEventsetting the intercepted variable in the method. If it returns true in the interception method, then intercepted is true. If it is true, mFirstTouchTarget=null at the time of action_down, then the dispatchTransformedTouchEventincoming child=null is directly called at this time. , So the event is handed over super.dispatchTouchEvent, and now it is treated as a view.

Interviewer: There is a viewgroup with a view inside. If the view does not distribute events in dispatchTouchView, and only intercepts touch events in action_move, how to distribute the actions from viewgroup to view?

At last

I have seen many technical leaders meet older programmers who are in a period of confusion during interviews, who are older than the interviewer. These people have some common characteristics: maybe they have worked for 5 or 6 years, or they write code to the business department repeatedly every day. The repetitive content of the work is relatively high, and there is no technical content. When asked about their career plans, they didn't have much ideas.

In fact, the age of 30 to 40 is the golden stage of a person’s career development. You must have your own plan for business expansion, technical breadth and depth enhancement, and it will help you have a sustainable development path in career development. Don't stand still.

Keep running, you will know the meaning of learning!

"Android Senior Architect Interview Guidance + 2021 Big Factory Interview Questions" free to receive

%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)**

[External link image is being transferred...(img-B416d0jS-1614249880147)]

Guess you like

Origin blog.csdn.net/chayel123/article/details/114099386