Android imitation wechat right slide return function example code
First, the renderings are as follows:
First, analyze the main technical points of the function. Right sliding is gesture judgment. Return is executed only when sliding to the full distance, and the position of finger pressing is on the far left of the screen (this also has a certain range). These can be realized by ontouchevent. Then there is the sliding effect when returning. Obviously, this is realized by the acitivty switching animation. All right, let's start after the analysis. The following code is:
This code is mainly used to judge whether the direction is right sliding when the finger slides on the screen. If so, use scrollto() to realize the sliding of the activity. When the sliding exceeds 300px, execute the return, and add the switching animation. Careful friends should find scrollto() The method slides only the content, not the view itself. How does it realize the sliding of the whole activity? Yes, if it is only the above part of the code, you can't understand the content of the previous interface when sliding, because our view is still there. Here, let's say that this view is the root layout of the activity I obtained. Well, since this view blocks us If you want to make the root layout transparent, just change the theme as follows:
Just add the above two lines of code to your topic.
It feels like it's done. In fact, it's not. As we said earlier, when the finger slides to the right from the far left of the screen, it actually slides. This involves event distribution. For example, the width of one button occupies the full screen. When our finger presses on the left side of the button, do we execute the click event or slide? At this time, you need to use dispatchtouchevent to deal with this problem. Here's the code:
The above code is that when our finger is pressed on the leftmost side of the screen, if it is less than 50 (this value can be set according to the demand), we think that the user needs to slide right, so we consume it and do not pass it to the next layer.
When no is less than 50, it is executed normally.
The whole process has been finished. Is it very simple! These codes are best implemented in the activity base class! This is convenient for management!
summary
The above is the example code of Android imitation wechat right slide return function introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!