Realization idea
====
By monitoring the state of the keyboard and the state of the commodity card, the translation y animation is made for the bullet screen area. The implementation idea is very simple. There are some difficulties below
-
Implementation method of monitoring keyboard state
-
How to get the height of the keyboard
Monitor keyboard pop-up and stow
========
Because Android can't monitor the keyboard, online solutions are judged indirectly by viewTreeObserver
view.viewTreeObserver.addOnGlobalLayoutListener { // Here, by judging the visible height of the screen, you can indirectly judge the pop-up and retraction }
Why can it be achieved in this way?
Let's take a look at when addOnGlobalLayoutListener was called back
Here, you need to understand the drawing process of View. The code is in line 2628 (API 29) of the ViewRootImpl class
if (triggerGlobalLayoutListener) { mAttachInfo.mRecomputeGlobalAttributes = false; mAttachInfo.mTreeObserver.dispatchOnGlobalLayout(); }
This callback will be triggered when a Window such as an Activity or Dialog completes the measurement and layout of the added layout. It can be inferred from this that the re layout of the Activity must be triggered when the keyboard pops up and retracts
Why does keyboard pop-up trigger the rearrangement of activities
Android provides the property of windowSoftInputMode for developers to configure the state of the keyboard and how the interface should adapt to the keyboard
// The current interface does not change. The keyboard is directly covered on the interface and will not be rearranged. Therefore, the GlobalLayoutListener callback will not be triggered android:windowSoftInputMode="adjustNothing" // Changing the size of the current layout to fit the keyboard will trigger the GlobalLayoutListener callback. This configuration is very suitable for the IM chat interface of wechat android:windowSoftInputMode="adjustResize" // Directly pushing the layout to the top will trigger the GlobalLayoutListener callback android:windowSoftInputMode="adjustPan" // By default, the interface can be swiped and will be set to adjustResize, otherwise adjustPan android:windowSoftInputMode="adjustUnspecified"
The pop-up of the keyboard will change the interface layout, so you can judge whether the keyboard pops up in the above way
How to judge whether the keyboard pops up
The keyboard pop-up will change the visible area of the current Window. If the current visible height is less than the original height, we judge that the keyboard pops up
You can obtain the visible area of the Window where the View is located through the methods in the View
view.getWindowVisibleDisplayFrame(Rect)
Then the original height minus the current height is the height of the keyboard
RecyclerView top gradient vanishing effect
=====================
RecyclerView provides this attribute to achieve this effect (it's hard to know this attribute when doing the project). android:requiresFadingEdge = "vertical", but after setting this attribute, android:overScrollMode="never" has no effect, so RecyclerView is rewritten here
// android:overScrollMode="never" style edgeEffectFactory = object : EdgeEffectFactory() { override fun createEdgeEffect(view: RecyclerView, direction: Int): EdgeEffect { return object : EdgeEffect(context) { override fun draw(canvas: Canvas): Boolean { return false } } } }
Finally, send benefits. Now pay attention to me and join the group chat. You can get information including source code analysis, custom View, animation implementation, architecture sharing, etc.
The content is moderately difficult and concise. It only takes more than ten minutes to read every day.
You can discuss with me, including fluent - underlying development - Performance Optimization - Mobile Architecture - Senior UI Engineer - NDK related professionals and video teaching materials, as well as more interview questions for you to get
CodeChina open source project address: https://codechina.csdn.net/m0_60958482/android_p7
The interview questions are waiting for you**
CodeChina open source project address: https://codechina.csdn.net/m0_60958482/android_p7
[external chain picture transferring... (img-HeDE5s7b-1630295407420)]