Android imitates IOS to realize sideslip return function (similar to wechat)

We all know that sideslip return operation is a common function in IOS. Generally, you can close the current interface by sliding your finger to the right near the left edge of the mobile phone screen. IOS system provides such an API, but how does Android implement it? I found many methods on the Internet and compared them. Personally, I think it is more convenient and easy to understand,

Let's talk about the previous effect first:

principle

The activity itself cannot slide, but we can create an illusion that the activity is sliding, making it appear that the activity is being slid by a finger. The principle is actually very simple. What we slide is actually the visible view element in the activity, and we set the activity to transparent. In this way, when the view slides over, because the bottom of the activity is transparent, we can see the following activities during the sliding process, so it looks like we are sliding the activity. Therefore, the activity sliding effect is divided into two steps:

Set activity transparent sliding view

realization

1. Set transparency:

Add the following code to the resource file values - > styles.xml to create a transparent theme.

Then set a transparent theme for the corresponding activity in the androidmanifest.xml file. For example, mainactivity:

2. Slide view:

Let's take a look at the hierarchy of the activity: the XML root view of the activity we use is not the root view of the activity. There is also a parent view on it, with the ID of android.r.id.content. Up one layer, there is also a view, which is a LinearLayout. In addition to placing the views we create, We also put things other than our XML, such as actionbar or title bar. At the next level, you will reach the root view of the activity - decorview structure

As shown in the figure below:

To slide the entire activity like IOS, it is obviously wrong to only slide the view we created in XML, because we also have title bar and actionbar, so we should slide decorview or the view on the penultimate layer.

To slide the view, we need to rewrite the onintercepttouchevent and ontouchevent of its parent window (of course, it is not impossible to use setontouchlistener, but if one of the child views consumes ontouch events, it will not be received), but the window creation process is beyond our control, The creation of decorview is beyond our intervention.

The solution is to create a swipelayout ourselves, and then artificially insert it into the top-level view and place it between the decorview and the LinearLayout below it. With the sliding of our fingers, we can constantly change the position of the child view of swipelayout, which was once the child view of decorview, so that we can control the sliding of activity. We call swipeLayout.replaceLayer in Activity's onPostCreate method to replace our SwipeLayout. The code is as follows:

Then write these as a swipeactivity, which requires the sideslip return activity to inherit swipeactivity, and swipeactivity to inherit fragmentactivity or activity. For example, mainactivity:

summary

In fact, during work, the development of a project will basically encapsulate a baseactivity. As long as baseactivity inherits swipeactivity, then all activities inheriting baseactivity can realize sideslip return. If there is one method in swipeactivity that does not need to realize this function, just call public void setswapeenabled (Boolean swipaenabled) Just pass a false parameter. Isn't it convenient!

In fact, another method is exposed: public void setswapeanywhere (Boolean swipeanywhere) sets whether to turn off the right slide at any position of the screen, because by default, it can only be returned by right slide at the left edge.

last

Attach the original GitHub: https://github.com/NashLegend/SwipetoFinishActivity

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>