Android realizes the function of navigation tab bar suspension in app

The first is the effect picture of the "hungry" navigation tab bar floating.

You can see that the "classification", "sorting" and "filtering" in the figure above will be suspended at the top of the app, and the state will change with the Scrollview (or not Scrollview, here let's treat the sliding UI control as Scrollview). I believe everyone can realize the floating function of the navigation tab bar. The tab bar will not slide out of the screen with the Scrollview, which increases the interaction and convenience with users.

Seeing the above effect, I believe everyone is eager to try. Let's start.

First of all, you should understand that the status change of the tab bar is to monitor the Scrollview sliding distance. As for how to get the sliding distance of Scrollview? Let's take a look at another article: "the method of realizing sliding distance listener in Scrollview in Android", which is not described here.

OK, according to the above, you can get the monitoring of Scrollview sliding. The next question to consider is how to make the tab bar achieve the effect of suspension? There are two methods given here. The first is to use WindowManager to dynamically add a view suspended on the top; The second is to reset the layout position of the tab bar as the Scrollview slides.

Let's take a look at the first @ R_ 301_ 1957 @, the first is XML layout.

Layout of activity_ main. xml:

Tab column layout, tab_ layout. xml:

Many blank linearlayouts in the above layout are mainly to lengthen the Scrollview. The rendering is like this:

Then let's take a look at oncreate (bundle savedinstancestate):

First, we add a sliding distance listener to Scrollview in oncreate (bundle savedinstancestate) and get a WindowManager object. Another thing to note is that we called getsupportactionbar() hide(); Removed the title bar (mainactivity inherits appcompatactivity). This is because the existence of the title block leads to the additional height of the title block when calculating the y-axis value of the suspended window (of course, you can also keep the title block, and then add the height of the title block when calculating ^ ^.

Then get the height of the tab column and the gettop () value in onwindowfocuschanged (Boolean hasfocus) for the following standby.

Then control whether to display or hide the floating window in the callback method onscroll (int scrolly) of the sliding listener.

The above code is relatively simple, so I don't need to describe it too much. The following two methods are removewindow() and showwindow():

These two methods are also very simple and annotated. I believe you can understand them.

Finally, don't forget to use Android manifest Apply for permission to display the suspended window in XML:

That's all for the whole code. Let's see the effect:

It is worth noting that if you use this method to realize the tab bar suspension function, there is a disadvantage, that is, if the app is not given the permission to display the suspension window, the function will become a chicken rib.

summary

The above is the whole content of this article. I hope the content of this article can be helpful to the study or work of Android developers. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>