android--imitation QQ side-slip Menu

Source: Internet
Author: User

First or first look at it, this example can find a lot of online, I mainly here is to do a summary and some code analysis, when it is their own in the learning process of an accumulation, because the same articles on the Internet a lot, so also can not explore the original source is where.

We can see from the fact that the main interface and menu is two diagram, just in the process of sliding, the menu gradually appeared, the main interface gradually disappear process, can achieve this effect of the control or more, such as: Girdview, ScrollView, such as scrolling view control. The Horizontalscrollview is used here.

We can see the basic information of Horizontalscrollview by looking at the API. Horizontalscrollview a container for layouts that lets users view the hierarchy of views using scrollbars, allowing the view structure to be larger than the phone's screen. Horizontalscrollview is a framework layout, which means that you can place a child view of the complete content that you want to scroll into the container, which can itself be a layout manager with a complex hierarchy. Generally use horizontal linearlayout as a sub-view, Enables the user to scroll through the entries displayed in it. The Horizontalscrollview only supports horizontal scrolling.

One, a custom class, used to inherit Horizontalscrollview.
public class Slidingmenu extends Horizontalscrollview {public Slidingmenu (context context) {Super (context, NULL, 0);} Public Slidingmenu (context context, AttributeSet Attrs) {Super (context, attrs, 0);} Public Slidingmenu (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);}}

After the creation of this class, we will find that there are three construction methods here, so let's introduce these three construction methods, which is helpful for understanding custom controls.

1. Only one parameter is constructed: it is usually called when Java code creates a view, and it is not called if it is a view populated from XML.

2. There are two constructor methods for the parameters: this is called when the XML is created but no style is specified.

3. How to construct three parameters: this is easy to understand, it is called when there is a specified style.

Ii. completion of the layout file

As I said at the beginning, the effect is that the main interface and menu two interfaces are arranged in parallel in the Horizontalscrollview layout container, and when there is a sliding event, one moves slowly, thus showing the other one.

So the completion of the layout file is relatively simple.

Activity_main.xml: (Where a section on custom attributes is explained later)

<com.ithaha.slidingmenu.slidingmenu xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+ Id/id_menu "xmlns:tools=" Http://schemas.android.com/tools "xmlns:zhy=" http://schemas.android.com/apk/res/ Com.ithaha.SlidingMenu "android:layout_width=" wrap_content "android:layout_height=" Fill_parent "Android:scrollbar S= "None" zhy:rightpadding= "100DP" > <linearlayout android:layout_width= "wrap_content" Android:la yout_height= "fill_parent" android:orientation= "horizontal" > <include layout= "@layout/layout_menu"/&G        T <linearlayout android:layout_width= "fill_parent" android:layout_height= "Fill_parent" a                ndroid:background= "@drawable/qq" > <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:onclick= "Togglemenu" android:text= "Toggle Menu"/&        Gt </LinearLayout>    </LinearLayout></com.ithaha.SlidingMenu.SlidingMenu> 

Layout_menu.xml:

Here are some simple menu display, because the code is too long and no special after the post, the specific can see the source code.

Third, complete the custom class, realize the main function

After completing the previous two steps, we can run the program to look at it first. After running the program, found that the interface can be left and right sliding, but the effect is too poor (below), so we need to rewrite some methods to make the interface more beautiful and reasonable.

          

As can be seen from just running, the interface picture size and screen size have obvious not collocation, so we first need to rewrite the first method is:

protected void onmeasure (int widthmeasurespec, int heightmeasurespec)

Evaluates the view and its contents to determine its width and height. Called by this method measure(int, int) , subclasses can be overloaded to provide a more accurate and efficient way of measuring their content size

protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {/** * shows the setting of a width */if (!once) {//Returns the view at the specified position. LinearLayout wrapper = (linearlayout) getchildat (0); ViewGroup menu = (viewgroup) wrapper.getchildat (0); ViewGroup content = (viewgroup) wrapper.getchildat (1);//Width of menu = width of screen-required margin to be left mmenuwidth = mscreenwidth-mmenurightpa dding;//half of the menu width mhalfmenuwidth = mmenuwidth/2;//reload width menu.getlayoutparams (). width = mmenuwidth; Content.getlayoutparams (). width = mscreenwidth;} Super.onmeasure (Widthmeasurespec, heightmeasurespec);}

Because it just defines the mmenurightpadding (the distance from the right edge of the menu interface), but does not actually give the value, so now the program runs out, just two full interface, if the mmenurightpadding assignment, Then there will be a better effect. But since mmenurightpadding we set it up as a custom property, we'll also review the use of custom properties here.

Simple use of custom attributes:

1. You need to declare a namespace first in the layout file: xmlns:ithaha= "Http://schemas.android.com/apk/res/com.ithaha.SlidingMenu"

The abbreviation for the Xmlns:xml namespace, in fixed format.

Ithaha: The prefix of the property name. Just as we use the control's own property, there is an android in front of the property name:

http://schemas.android.com/apk/res/: For fixed format

Com.ithaha.SlidingMenu: The package name of the loaded property

2. Create a new Attrs.xml file under res/values/

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <!--declaration Property set name--    < Declare-styleable name= "Slidingmenu" >                <!--declares a property name is the rightpadding type is dimension--> <attr name=        "Rightpadding" format= "Dimension"/>            </declare-styleable></resources>

3. Load the attributes in the. java file (see source code)

4. Use in the layout file.

Iv. perfection of the final function

After the completion of the previous three steps, can achieve a basic effect, but the interface of the switch is their own on the screen sliding how much distance, the interface switch how much, use is not very convenient, so we continue to improve.

        public boolean ontouchevent (motionevent ev) {int action = ev.getaction (); switch (action) {//up), If the display area is larger than half of the menu width, otherwise hide case MotionEvent.ACTION_UP:int scrollx = GETSCROLLX (), if (Scrollx > Mhalfmenuwidth) { This.smoothscrollto (mmenuwidth, 0); isOpen = false;} else {this.smoothscrollto (0, 0); IsOpen = true;} return true;} return super.ontouchevent (EV);}

By judging the distance the finger is sliding on the screen, and then moving the interface directly, you can achieve the desired effect.

Five, source code download

Click I download O (︶^︶) o

android--imitation QQ side-slip Menu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.