(3) Slidingmenu Open source framework for Slidemenu in the project use method +fragment

Source: Internet
Author: User

Now we're going to start by making the skid bar, and then the bottom.

1. Side-slip menu

We have introduced the library of the slide-off menu last time, and now we are going to use this thing.

If I were to use it, mainactivity would not inherit the activity, but inherit slidingfragmentactivity.

Now let's analyze it.

(1) In fact Setcontentview (r.layout.activity_main); This code just put activity_ Main.xml this interface to render, and this also becomes the content page, and then the content page has a button to point out a thing, this is the side-slide menu.

(2) We already have setcontentview, and then we have to design the view of Setbehindcontentview, which is the hidden side-slip menu.

Let's start by writing about the layout of this slide-off menu Menu.xml

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

To use the side-by-side menu, to get a side-by-side menu, let's analyze what to do with the slide-slip menu.

1. Get side-slip menu

Slidingmenu sm = Getslidingmenu ();

2. Set the side-slip menu to come out from the left or from the right.

Sm.setmode (Slidingmenu.left);

3. After setting the sliding menu, the content page shows the remaining width.

Sm.setbehindwidthres (R.dimen.slidingmenu_offset);

4, set the shadow of the sliding menu, set the Shadow, the shadow needs to start, especially dark, slowly fade

Sm.setshadowdrawble (R.drawable.shadow);

5. Set the width of the shadow

Sm.setshadowwidth (R.dimen.shadow_width);

6. Set the range of the sliding menu

First parameter slidingmenu.touchmode_fullscreen can slide in full screen

The second parameter slidingmenu.touchmode_margin can only be slid at the edge of the

Three parameters Slidingmenu.touchmode_none cannot slide

Sm.settouchmodeabove (slidingmenu.touchmode_fullscreen);


(3) After a series of method calls above, we have integrated the sliding menu. Now all we have to do is initialize the data in the sliding menu. Set up a package to put fragment.

We build a menufragment class to inherit fragment. Then we rewrite the inside of the 3 methods, respectively, is onactivitycreated( ); OnCreate (); Oncreateview ();

Then we create the menu content view, which is a ListView.

Then, in Oncreateview (), the method uses Layoutinflater to load the list_view.xml in,

View = Layoutinflater.from (Getactivity ()). Inflate (R.id.list_view,null);

return view; //NOTE: The context of the fragment is obtained by getactivity ()


Note: Learning fragment first to understand the life cycle of fragment in depth. (Official documentation available)

Fragment actually can not exist independently, it is attached to the activity, and then, that is, its life cycle method and activity very much like,fragment first Go is the OnCreate () method, and then go Oncreateview (); method, then go onactivitycreated ();

< Span style= "font-family: ' Microsoft Yahei '; font-size:18px "> Next up, we're in onactivitycreated () The data of the ListView is loaded through adapter, if the data is not many, simple, can consider directly with the system adapter, we can define a arrayadapter<string> on the line, Then pass a list<string> to the adapter.

Finally, we want to use this fragment, then we are in the mainactivity inside new A fragment, then we can open a transaction through the Getsupportfragmentmanager (), and then to replace. There are 3 parameters, the first one is the ID I want to replace, the second is the fragment I want to replace, and the third is the value to give it a binding.

Through a series of operations, our slidemenu+fragment is done.











(3) Slidingmenu Open source framework for Slidemenu in the project use method +fragment

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.