I saw a friend who was interviewing Ali write in his personal ability:



More interview content, technical dry goods, technical exchange:



Star my GitHub

    1. Familiar with Java multi-threaded programming, familiar with Android thread and thread, process and process communication mechanism, familiar with Android model adaptation, UI layout optimization, can independently develop Android applications;
    1. Familiar with custom controls, ViewGroup bottom measurement, drawing, rendering have a deep understanding, and their own RecyclerView recycling ideas, designed the form control
    1. Familiar with Framework API, Handler, Message, Binder, AIDL, BroadcastReceiver and other underlying APIS, and have a certain understanding of the rationale and features of virtual machines;
    1. Proficient in the use of third-party frameworks, such as Baidu voice API, Baidu location API, Umeng sharing API, Ali Cloud push API;
    1. Master the core principles of the most popular frameworks on the Internet such as OkHttp, Retrofit, Glide, ButterKnife, ARouter, EventBus, LiveDataBus, etc.
    1. Familiar with APP performance optimization, such as: lag optimization, memory optimization, APP package optimization, Crash monitoring;
    1. Familiar with APP componentized development and Hybrid development;

So the interviewer starts the interview rapid-fire

Interviewer: See your resume on the custom control more understanding, especially RecyclerView has seen the corresponding source code, RecyclerView reuse mechanism is what

About RecyclerView high frequency surface pilot:

  1. RecyclerView supports many different types of layout, how do they cache, and find

    Reclaim pool list two-dimensional array cache type store

  1. Why do you need adapters for RecyclerView? What is your understanding of adapters

    The UI is separated from the concrete business logic

  1. How to determine the number of RecyclerView screen loading, how does he do not display Item cache into memory

    The border

  2. The most difficult knowledge of mathematics

  3. Have read RecyclerView boundary judge source code? Just a quick word about his judgment mechanism, when should it be recycled

Three of ali’s four rounds of interviews asked about RecyclerView. Interview points are different, there are principles, nested questions, cache implementation, but in the end are all the same, all the questions are gathered in how to do RecyclerView performance optimization?

1.1 RecyclerView the first layout, will pre-layout occur?

Pre-layout is not triggered for the first time. Pre-layout will only be triggered each time notify change. The purpose is to record the coordinates of ViewHolder at each position on the screen by saveOldPosition method. After relayout, the animation effect of Item will be realized by comparison. For example:

1.2 What should I pay attention to if I customize LayoutManager?

In the dispatchLayoutStep1 phase of RecyclerView, Will call the custom LayoutManager supportsPredictiveItemAnimations method whether in some state display predictive animation. Implementation of the following LinearLayoutManager:

  @Override
    public boolean supportsPredictiveItemAnimations() {
    return mPendingSavedState == null && mLastStackFromEnd == mStackFromEnd;
  }
Copy the code

If supportsPredictiveItemAnimations returns true, then in the LayoutManager autotype onLayoutChildren method is called twice: once in the pre – layout, another is real – layout.

Because there will be pre-layout and real-layout, so in the custom LayoutManager, we need to distinguish between the two layouts according to the return value of isPreLayout method in RecyclerView.State. For example, onLayoutChildren in the LinearLayoutManager has the following judgment:

image

There is a comment in the code above:

if the child is visible and we are going to move it around, we should layout extra items in the opposite direction to make sure new items animate nicely instead of just fading in

It means that if the item currently being updated is visible, an additional item needs to be filled in the pre-layout stage to ensure that the item in the invisible state can slide smoothly onto the screen.

1.3 Examples

For example, in the figure below, click Item2 to remove it. After notifyItemRemoved, Item5 was not added to RecyclerView before pre-Layout, but after pre-Layout, Item5 was added to RecyclerView after layout



item5
item3
item4


If the custom LayoutManager does not implement pre-layout or does not implement it properly, then when Item2 is removed from the screen, only Item3 and Item4 will be smoothly moved, and Item5 will simply appear on the screen, as shown below:



item5
item3
item4

1.5 When is ViewHolder cached into RecycledViewPool?

There are mainly the following two situations:

  • whenItemViewIs swiped off the screen, andCachedViewIs full, thenViewHolderIt’s going to be cachedRecycledViewPoolIn the
  • If data changes, the execution is completedisappearrancetheViewHolderIt’s going to be cachedRecycledViewPoolIn the
1.6 Relationship between CachedView and RecycledViewPool

When an ItemView is swiped off the screen, it is saved in CachedView by default. CachedView has a default size of 2 and can be changed using the setItemViewCacheSize method. When CachedView is full and a new ItemView slips out of the screen, it will force CachedView to transfer the ViewHolder of cache to RecycledViewPool according to FIFO rules. The effect can be seen in the following figure:



RecycledViewPool
RecycledViewPool

 RecyclerView.getRecycledViewPool().setMaxRecycledViews(int viewType, int max);
Copy the code

1.7 Differences between CachedView and RecycledViewPool

The ViewHolder cached in the CachedView does not clean up relevant information (such as position, state, etc.), so a ViewHolder that has just been removed from the screen can be retrieved and displayed in the CachedView when it is moved back to the screen. No rebinding (bindViewHolder) is required.

And the ViewHolder cached in RecycledViewPool will be cleaned state and location information, so it is necessary to call bindViewHolder binding data again to find the ViewHolder from RecycledViewPool.

1.8 From what aspects did you optimize RecyclerView?

I summarized several points, mainly can optimize RecyclerView from the following aspects:

  • Try to put complex data processing operations into asynchrony. The data that RecyclerView needs to display is often obtained from the remote server request, but after the network request gets the data, the data needs to be flattened, and the best data format is returned to the UI thread as far as possible.

  • Optimize the RecyclerView layout to avoid using it with ConstraintLayout

  • For fast sliding events, you can use addOnScrollListener to add a listener for fast sliding to stop loading data when the user is fast sliding.

  • If the ItemView has a fixed height, use setHasFixSize(true). In this way, RecyclerView can directly calculate the height in onMeasure stage, without the need to calculate the height of sub-ItemView for many times. This situation is very significant for vertical RecyclerView nested transverse RecyclerView.

  • When UI is Tab feed stream, consider using RecycledViewPool to implement cache sharing of multiple RecyclerViews.

More interview content, technical dry goods, technical exchanges: Star on my GitHub

Original is not easy, little praise praise ~