"Android UI design and development" phase 17th: Sliding menu bar (ii) example of an open source project Slidingmenu

Source: Internet
Author: User

Through the explanation of the previous article, I believe that we have a relatively preliminary understanding of the Open source project Slidingmenu (do not understand can refer to the

An article), then from this chapter, the Slidingmenu will be the center of gravity, to bring you a very rich sample demonstration, so that everyone on

Slidingmenu has a more in-depth understanding and how to achieve some of the more stunning sliding effects.

This article will give you an example of 7 different effects, each of which contains the most basic methods of use, from easy to difficult, and step-up. Can

So that beginners can quickly master the use of Slidingmenu method. The next article will bring you three different animation effects to implement the sliding menu bar

The slide.

For these examples, there are some examples from the Slidingmenu open source project, but for beginners, to run these examples to see

, you have to import a lot of libraries, and it is very troublesome to use them. Bloggers here in order to be able to facilitate the better study, we have each example is a single

Took out alone, and added some very detailed comments, so that beginners can understand at a glance, then the purpose of bloggers to achieve.

OK, not much to say, I hope you can continue to support!

One, example

First of all, Bo Master has always been the style, so that we can give you a general understanding of the effect of the realization, you can get their own want to go

The effect of the implementation.

1. Example One

The example is mainly to demonstrate the different effects of changing the sliding menu by changing different values, and the values you can set are:

<1> set the location of the slide menu display (left, right, or both sides);

<2> Set the touch screen mode (full screen touch to open the swipe menu, Edge touch to open the swipe menu or touch cannot open the swipe menu);

<3> sets the effect of zooming when the slide menu is sliding and turns off the effect (the larger the value is, the more obvious it is);

<4> set the shadow effect when sliding the slide menu and turn it off (the larger the value, the greater the effect);

<5> set the fade-in effect when sliding the slide menu (the larger the value, the greater the effect).

                               a                                                                                                              two

2. Example Two

The main example is to teach you to implement a simple placement on the left side of the slide menu bar effect.

One

3. Example Three

This example implements two views on both sides of the left and right, and can be opened by swiping through the screen.

4. Example Four

This example demonstrates the interface content that changes the main view through fragment.

5. Example Five

The example also changes the interface content of the main view through fragment, but the difference is that the picture is stored in the list, and clicking on the list image will

is displayed by compression.

6. Example Six

In this example, you use the Viewpager class to store multiple fragment in Viewpager and display it in the contents of the main interface view.

                                                              a                                                                                                                                                       II

7. Example Seven

This example mainly demonstrates that when the touch screen is sliding, the above title bar TitleBar does not slide along with it, but slides in the middle of the content view. (Evernote achieves this effect.)

Second, the code explanation

Because of the many examples, bloggers I do not explain the code of each example, here is a sample of the most basic to explain a

Under At the end of the article, I will pack all the examples for everyone to download the study, there are some detailed comments, I believe you can see it.

1. Project Structure Chart

2. Content_frame.xml layout

Let's start with the layout file, why do we define a content_frame.xml here? Primarily to define a virtual view, it is convenient for fragment to replace the view to achieve the effect that fragment view content can display on the screen.

<?xml version= "1.0" encoding= "Utf-8"?> <framelayout  xmlns:android= "http://schemas.android.com/apk/ Res/android "      android:id=" @+id/content_frame "      android:layout_width=" Match_parent "       android:layout_height= "Match_parent"/>  

As you can see here, a framelayout layout is defined, with an ID attached to it so that the layout object can be found in the system.

3. Menu_frame.xml Layout

This layout file is used to store the view interface after the sliding menu is opened.

<?xml version= "1.0" encoding= "Utf-8"?> <framelayout  xmlns:android= "http://schemas.android.com/apk/ Res/android "      android:id=" @+id/menu_frame "      android:layout_width=" Match_parent "       android:layout_height= "Match_parent"/>  

4, List.xml

This list layout file will be used in the listfragment.

<?xml version= "1.0" encoding= "Utf-8"?> <listview  xmlns:android= "http://schemas.android.com/apk/res/ Android "      android:id=" @android: id/list "      android:layout_width=" Match_parent "       Android:layout_height= "Match_parent"      android:paddingleft= "@dimen/list_padding"       Android:paddingright= "@dimen/list_padding"/>  

5, Row.xml

Used to hold the picture and text in the list.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "50DP"android:orientation= "Horizontal" > <ImageView Android:id= "@+id/row_icon"Android:layout_width= "50DP"Android:layout_height= "50DP"android:padding= "10DP"android:src= "@drawable/ic_launcher"/> <TextView Android:id= "@+id/row_title"Android:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "1"android:gravity= "Center_vertical"android:padding= "10DP"Android:text= "Medium Text"android:textappearance= "@android: Style/textappearance.medium"/> </LinearLayout>

6. Shadow.xml Resources

This resource file is a graphic used to implement the shadow effect, using a gradient drawing effect.

<?xml version= "1.0" encoding= "Utf-8"?> <shape  xmlns:android= "http://schemas.android.com/apk/res/ Android ">        <gradient          android:endcolor=" #33000000 "          android:centercolor=" # 11000000 "          android:startcolor=" #00000000 "/>    </shape>  

7, Samplelistfragment class

Finish the layout file and let's take a look at the Java file. This class is used to display the list in the interface.

 Packagecom.yangyu.myslidingmenudemo02; ImportAndroid.content.Context; ImportAndroid.os.Bundle; Importandroid.support.v4.app.ListFragment; ImportAndroid.view.LayoutInflater; ImportAndroid.view.View; ImportAndroid.view.ViewGroup; ImportAndroid.widget.ArrayAdapter; ImportAndroid.widget.ImageView; ImportAndroid.widget.TextView; /**  * @authorYangyu * Function Description: List fragment, used to display the list view*/   Public classSamplelistfragmentextendslistfragment { PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {returnInflater.inflate (R.layout.list,NULL); }         Public voidonactivitycreated (Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate); Sampleadapter Adapter=NewSampleadapter (Getactivity ());  for(inti = 0; I < 20; i++) {Adapter.add (NewSampleitem ("Sample List", Android.          R.drawable.ic_menu_search));      } setlistadapter (adapter); }        Private classSampleitem { PublicString tag;  Public intIconres;  PublicSampleitem (String tag,inticonres) {               This. Tag =tag;  This. Iconres =Iconres; }      }         Public classSampleadapterextendsArrayadapter<sampleitem> {             PublicSampleadapter (Context context) {Super(Context, 0); }             PublicView GetView (intposition, View Convertview, ViewGroup parent) {              if(Convertview = =NULL) {Convertview= Layoutinflater.from (GetContext ()). Inflate (R.layout.row,NULL); } ImageView icon=(ImageView) Convertview.findviewbyid (R.id.row_icon);              Icon.setimageresource (GetItem (position). Iconres); TextView title=(TextView) Convertview.findviewbyid (r.id.row_title);                Title.settext (GetItem (position). tag); returnConvertview; }        }  }  

8, Mainactivity class

This is the entry class for the entire program, and in this class, the properties of the Slidingmenu are set.

 Packagecom.yangyu.myslidingmenudemo02; ImportAndroid.os.Bundle; Importandroid.support.v4.app.FragmentActivity; ImportCom.jeremyfeinstein.slidingmenu.lib.SlidingMenu;  Public classMainactivityextendsfragmentactivity {Privateslidingmenu menu; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Set TitleSettitle ("Attach"); //Initialize the swipe menuInitslidingmenu (); }        /*** Initialize sliding menu*/      Private voidInitslidingmenu () {//Setting the main interface viewSetcontentview (r.layout.content_frame); Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.content_frame,Newsamplelistfragment ()). commit (); //Set property values for a slide menumenu =NewSlidingmenu ( This);          Menu.settouchmodeabove (Slidingmenu.touchmode_fullscreen);          Menu.setshadowwidthres (R.dimen.shadow_width);          Menu.setshadowdrawable (R.drawable.shadow);          Menu.setbehindoffsetres (R.dimen.slidingmenu_offset); Menu.setfadedegree (0.35f); Menu.attachtoactivity ( This, slidingmenu.sliding_content); //set the view interface for the slide menuMenu.setmenu (r.layout.menu_frame); Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.menu_frame,Newsamplelistfragment ()). commit (); } @Override Public voidonbackpressed () {//Click the Back button to close the swipe menu        if(Menu.ismenushowing ()) {menu.showcontent (); } Else {              Super. onbackpressed (); }      }    }  

It is almost over here, the next article will bring you to open the sliding menu to achieve the animation effect, I hope you can continue to pay attention!

Sample source Code

The compressed package contains all the project examples:

Each project contains the source code, the implementation, and the APK that can be installed and run directly:

Special Note: When you run the project, do not forget to import the Slidingmenu_library project, use the method please refer to the previous article.

"Android UI design and development" phase 17th: Sliding menu bar (ii) example of an open source project Slidingmenu

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.