[Android] News client introduced Slidingmenu

Source: Internet
Author: User

Download slidingmenu,https://github.com/jfeinstein10/SlidingMenu

Import Library

Our project right- ==>properties==>android==>library add third-party projects as dependent

This error occurred:Found 2 versions of Android-support-v4.jar in the dependency list

Because the Android-support-v4.jar package in a third-party project conflicts with our project, It is possible to overwrite third-party projects with our jar packages.

The current activity inherits slidingfragmentactivity, overriding the onCreate () method,

Call Setbehidcontentview (), set the slide menu's layout file, Parameters: Resource ID

Gets the slidingmenu object, via the getslidingmenu () method

Call the SetMode () method of the slidingmenu Object , set the left underline, parameter:slidingmenu.left

Call the slidingmenu object's setbehindwidthres (), set the menu width, parameters: Width dimen Resource

Call the setshadowdrawable () method of the slidingmenu Object , set the menu shadow, parameters:shape resource file

Shape resource file, add root node <shape>

Add gradient node,<gradient>, set start color android:startcolor="#000000"

Set the middle color,android:centercolor="2d000000"

Set the end color,android:endcolor="5a000000"

Call the slidingmenu object's setshadowwidth (), set the width of the shadow, parameter: Width

Call the Settouchmodeabove () method of the slidingmenu object to set the range of the slide,

Parameter:slidingmenu.touchmode_fullscreen fullscreen

Slidingmenu.touchmode_none not allowed to slide

Call requestwindowfeature (), parameter:window.feature_no_title, remove TITLE

Create Fragment

Create a new package xxx.fragment, create a new menufragment, inherit the v4 in the package Fragment

Rewrite onCreate ()

Override Oncreateview (), pass in layoutinflater Object

Create a new layout file,list_view.xml, put a ListView control inside, call the layoutinflater Object Inflate () method, turn into View Object

Rewrite onactivitycreated ()when activity is created

Call the View object's Findviewbyid ()to get the ListView Object

Call the Setadapter () method of the ListView Object , set the adapter, parameter:Adapter Object

Get arrayadapter Object, constructor method, Parameters: Context, layout resource, Text control ID, data List

Context (getactivity ()), the layout resource uses Android for the system . R.layout.simple_list_item1

Text ID android. R.id.text1, Data

Replace layout

In activity , get the Fragment object and Replace the menu layout with this Fragmenu

Get Fragment object,new Menufragment ()

Call getsuportfragment (). BeginTransaction (). replace (). Commit ( ) to substitute

Replace () method, Parameters: Layout ID,fragment object

Mainactivity.java

 Packagecom.qingguow.news;ImportCom.jeremyfeinstein.slidingmenu.lib.SlidingMenu;Importcom.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;Importcom.qingguow.news.fragment.MenuFragment;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.Window; Public classMainactivityextendsslidingfragmentactivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title); //Setting the Slide menuSetbehindcontentview (R.layout.menu);        Setcontentview (R.layout.activity_main); Slidingmenu SM=Getslidingmenu ();        Sm.setmode (Slidingmenu.left); Sm.setbehindwidth (200);        Sm.setshadowdrawable (R.drawable.shape); Sm.setshadowwidth (5);                Sm.settouchmodeabove (Slidingmenu.touchmode_fullscreen); //Replace FragemntMenufragment menufragment=Newmenufragment ();    Getsupportfragmentmanager (). BeginTransaction (). replace (R.id.ll_left_menu, menufragment). commit (); }}

Menufragment.java

 Packagecom.qingguow.news.fragment;Importjava.util.ArrayList;Importjava.util.List;ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListAdapter;ImportAndroid.widget.ListView;ImportCOM.QINGGUOW.NEWS.R; Public classMenufragmentextendsFragment {Privateview view; //Loading layout Files@Override PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancest ATE) {View= Inflater.inflate (R.layout.list_view,NULL); returnview; }    //setting up the ListView data@Override Public voidonactivitycreated (@Nullable Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate); ListView ListView=(ListView) View.findviewbyid (R.id.lv_menu); List<String> Data=NewArraylist<string>(); Data.add (News); Data.add (Found); Data.add (Attention); ListAdapter Adapter=NewArrayadapter<>(Getactivity (), Android. R.layout.simple_list_item_1, Android.        R.ID.TEXT1, data);            Listview.setadapter (adapter); }}

[Android] News client introduced 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.