(3) how to use SlideMenu of the SlidingMenu open-source framework in the project + Fragment, slidemenu

Source: Internet
Author: User

(3) how to use SlideMenu of the SlidingMenu open-source framework in the project + Fragment, slidemenu

Now, we will first make the slide bar and then make the bottom.

1. Slide menu

We have introduced the library of the slide menu last time. Now we need to use this item.

If I want to use it, MainActivity will not inherit Activity, but SlidingFragmentActivity.

Now let's analyze

(1) setContentView (R. layout. activity_main); this code only presents the activity_main.xml interface. This is also a content page. Then, a button is clicked on the Content Page to display something, this is the slide menu.

(2) We already have setContentView. Then we have to design setBehindContentView, which is the View of the hidden slide menu.

Next, let's write down the layout menu. xml of this slide menu.

<?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 slide menu, you must first obtain the slide menu. Next, let's analyze the steps to use the slide menu and what to do.

1. Get the slide menu

SlidingMenu sm = getSlidingMenu ();

2. Set whether the slide menu is displayed on the left or on the right.

Sm. setMode (SlidingMenu. LEFT );

3. Set the remaining width on the content page after the sliding menu is displayed.

Sm. setBehindWidthRes (R. dimen. slidingmenu_offset );

4. Set the shadow of the sliding menu and set the shadow. The shadow must be particularly dark and fade slowly at the beginning.

Sm. setShadowDrawble (R. drawable. shadow );

5. Set the shadow width.

Sm. setShadowWidth (R. dimen. shadow_width );

6. Set the sliding menu range.

// The first parameter SlidingMenu. TOUCHMODE_FULLSCREEN can be swiped in full screen.

// The second parameter SlidingMenu. TOUCHMODE_MARGIN can only slide at the edge

// Three parameters SlidingMenu. TOUCHMODE_NONE cannot be slide

Sm. setTouchModeAbove (SlidingMenu. TOUCHMODE_FULLSCREEN );

(3) After a series of method calls, we integrate the sliding menu. Now we need to initialize the data in the sliding menu. Create a package and place it in fragment.

We create a MenuFragment class to inherit Fragment. Then we will rewrite the three methods in them, namely onActivityCreated (); onCreate (); onCreateView ();

Then we create a menu content view, that is, a ListView.

Next, use LayoutInflater in the onCreateView (); Method to load list_view.xml,

View = LayoutInflater. from (getActivity (). inflate (R. id. list_view, null );

Return view; // Note: The Fragment context is obtained through getActivity ().

Note: To Learn Fragment, you must first have a deep understanding of the Fragment lifecycle. (See the official documentation)

Fragment actually cannot exist independently. It is attached to the Activity. Then, in other words, its life cycle method is similar to Activity. Fragment first follows the onCreate () method, then, use onCreateView (); method, and then onActivityCreated ();

Next, we load the ListView data through the Adapter in onActivityCreated (). If there is not much data, it is simple. You can directly use the system Adapter, we can define an ArrayAdapter <String>, and then send a List <String> to the Adapter.

Finally, we want to use this Fragment, so we will create a new Fragment in MainActivity. Then we can start a transaction through getSupportFragmentManager () and replace it. There are three parameters, the first is the ID to be replaced, the second is the Fragment to be replaced, and the third is to bind a value to it.

Through a series of operations, our SlideMenu + Fragment is complete.

Related Article

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.