Android uses the sliding menu framework to achieve the sliding menu effect

Before that, I introduced to you the implementation of the simplest sliding menu in history. I believe you all remember. If you forget the implementation principle or friends who haven't seen it, please read the previous article, the sideslip effect implementation code of Android imitation Renren client sliding menu, which is the simplest sideslip implementation in history, because the sliding menu framework we want to implement today is also based on the same principle.

It was also mentioned at the end of the previous article that if many activities in your application need to add the function of sliding menu, each activity needs to write hundreds of lines of code to achieve the effect. No matter how simple the sliding menu implementation scheme is, it is useless. Therefore, we are going to implement a sliding menu framework today, and then we can introduce the sliding menu function in any activity for one minute.

First, let's talk about the implementation principle. It's a sliding menu framework. In fact, it's very simple. We customize a layout, implement the sliding menu function in the custom layout, and then as long as we introduce our customized layout into the layout file of the activity, the activity will have the function of sliding menu. The principle is finished, isn't it very simple? Let's do it now.

Create a new Android project in eclipse called renrenslidinglayout.

Create a new class called slidinglayout, which inherits from LinearLayout and implements the ontouchlistener interface. The specific code is as follows:

See here, I believe you will feel very familiar with these codes. Yes, basically, these codes are similar to those in the previous article, except that these codes were written in the activity before, and now we move to the custom view.

Then let me explain the different parts from before. We can see that the onlayout method is rewritten here, using the layout obtained by getchildat (0) as the left layout and the layout obtained by getchildat (1) as the right layout. The width of the left layout is redefined as the screen width minus leftlayoutpadding, and the width of the right layout is redefined as the screen width. Then offset the left layout out of the screen so that only the right layout can be seen. Therefore, we can also see here that the precondition for using the slidinglayout layout is to provide two sub elements for the layout. The first element will be offset from the screen as the left layout, and the second element will be displayed on the screen as the right layout.

Then let's take a look at the setscrollevent method, which takes a view as a parameter and binds a touch event to the view. What does that mean? Let's imagine a scenario. If the layout on the right is a LinearLayout, I can control the display and hiding of the layout on the left by listening to the touch event on the LinearLayout. However, if a listview is added to the LinearLayout of the layout on the right, and the listview is full of the entire LinearLayout, the LinearLayout will not be touched again. At this time, we need to register the touch event on the listview. The setscrollevent method provides a registration interface, and the touch event will be registered on the incoming view.

Finally, there is an unfamiliar method, isbindbasicplayout. This method is to judge whether the view that registers the touch event is one of the four basic layouts. If so, it returns true, otherwise it returns false. This method plays a very important role in the entire slidinglayout. It is mainly used to control whether the ontouch event returns true or false, which will affect the availability of the view function in the layout. As Android event forwarding mechanism is involved in it, there are many contents, so I won't explain it in detail here. I will consider writing an article to introduce Android event mechanism in the future. Here, simply remember that if it is a basic layout, it will return true, otherwise it will return false.

OK, our slidinglayout is finished. Next is the moment to witness the miracle. Let's see how to introduce the sliding menu function into the activity in one minute.

Create or open the activity under the layout directory_ Main.xml file, add the following code:

We can see that under the root layout, we introduced the custom layout com.example.slide.slidinglayout, and then added two child elements, a relativelayout and a LinearLayout. Relativelayout is relatively simple, so a textview is added. We have added a button and a listview in LinearLayout.

Then create or open mainactivity as the main activity of the program and add code:

The above code focuses on calling the setscrollevent method of slidinglayout to register the touch event for listview. At the same time, a click event is added to the button to realize the function of clicking to display the left layout and then clicking to hide the left layout.

Finally, the old rule is to give the code of androidmanifest.xml:

Well, now let's run it. First, when the program is opened, the layout on the right is displayed. Slide your finger to the right on the interface to see the layout on the left.

When the layout on the left is fully displayed, the effect diagram is as follows:

In addition, clicking the menu button can also control the display and hiding of the layout on the left. You can try it yourself.

If you use a custom layout, you can add the sliding menu function to any activity in a simple way. Even if you have more activities, you don't have to be afraid. It's appropriate to introduce the sliding menu in one minute.

To sum up, adding a sliding menu function to an activity requires only two steps:

1. Introduce our customized layout into the layout of acitivty, and add two direct child elements to the layout.

2. Register a touch event for a view through setscrollevent method in activity.

Well, that's the end of today's explanation. If you have any questions, please leave a message below.

Please click here to download the source code

Supplement:

Since this article was written earlier, there were still many problems with the sliding menu written at that time. Later, I made many changes to the above code and wrote a modified version of the sliding menu. This version mainly corrected the following contents:

1. Change the sliding mode to cover type.

2. The listview will not easily slide out of the menu when scrolling up and down.

3. Shield events on the content layout when sliding.

4. When the menu layout is displayed, click the content layout on the right to hide the menu.

5. Fix the bug that the menu may be displayed briefly and then disappear instantly when the program is just opened.

Revised source code download, please click here

In addition, if you are interested in the two-way sliding menu, please turn to the example code of Android to realize the two-way sliding effect

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