Android drawerlayout Layout class with Sideslip (1) _android

Source: Internet
Author: User
Tags event listener

Drawerlayout As the name implies is a management layout. You can use it in a similar way to other layout classes.
Drawerlayout with a sliding function. As long as the layout in accordance with the provisions of drawerlayout layout, you can have the effect of sideslip.
Directly drawerlayout as the root layout, and then its internal

The first view is the content area

The second view is the left menu  

The third view is the right side sideslip Menu

The third one is currently optional.
The packages used are as follows:
import android.support.v4.widget.DrawerLayout;

Sometimes when you use these packages, you get an error. This time make sure ANDROID.SUPPORT.V4 is not the latest version.
Can update the support package, the file is stored in the Sdk/extres/support.
Then you can pass Eclipse>project right click>android tools>add Support Library ...
Or you can copy the files directly to the Libs folder in project.

 <android.support.v4.widget.drawerlayout xmlns:android=" http:// Schemas.android.com/apk/res/android "android:id=" @+id/drawer_layout "android:layout_width=" Match_parent "Android: layout_height= "Match_parent" > <framelayout android:id= "@+id/content_frame" android:layout_width= "Match_" Parent "android:layout_height=" match_parent "/> <listview android:id=" @+id/left_drawer "android:layout_width=" "240DP" android:layout_height= "match_parent" android:layout_gravity= "Start" android:choicemode= "SingleChoice" Andr Oid:divider= "@android: Color/transparent" android:dividerheight= "0DP" android:background= "#111"/> </ Android.support.v4.widget.drawerlayout> 

Drawer positioning and layout is controlled using the ' android:layout_gravity attribute on ' child views ' corresponding to Which side of the view you want the drawer to emerge from:left or right.
  (Or start/end on platform versions that support layout direction.)
  That is,
  android:layout_gravity= "Start" corresponds to the menu on the left to the right, left/ Start (right/end)
then from the layout file:
 framelayout is the content area, ListView is the left menu.
We need to do a fragment to load content:  

public class Pagefragment extends Fragment {public
  final static String Item_position_number = "Item_position_num"; 
   public pagefragment () {}
  @Override public
  View Oncreateview (layoutinflater inflater, ViewGroup container,
    Bundle savedinstancestate) {
   View Convertview = inflater.inflate (r.layout.page_fragment_layout, null);
   TextView TV = (TextView) Convertview.findviewbyid (R.id.textview);
   int num = getarguments (). GetInt (Item_position_number);
   Get list data from Res/array
   string[] dynastylist = Getresources (). Getstringarray (R.array.list_item);
   Tv.settext (Dynastylist[num]);
   Return Convertview
  }
 }

It can be seen in the code that when we select SelectItem in the left menu, the corresponding values are displayed to the content area.
The Page_fragment_layout.xml in the code is just one textview in the framelayout, so no code is attached.
Next we need to populate the ListView with data.

Private ListView menulist;
Private string[] mmenutitles;
Private string[] historytitles;
Private string[] musictitles;
Private string[] movietitles;
     Private string[] listtitles;
  History bar Historytitles = Getresources (). Getstringarray (R.array.history);
  Music Bar Musictitles = Getresources (). Getstringarray (R.array.music);
  Movie Bar movietitles = Getresources (). Getstringarray (R.array.movie);
  Title Array mmenutitles = Getresources (). Getstringarray (R.array.title);

  Each title Listtitles = Getresources (). Getstringarray (R.array.list_item);
  Drawlayout = (drawerlayout) Findviewbyid (r.id.drawer_layout);

  Menulist = (ListView) Findviewbyid (R.id.left_menu);
  Set menu Shadow Effects//Drawlayout.setdrawershadow (R.drawable.drawer_shadow,//Gravitycompat.start);
  list<item> list = new arraylist<item> ();
  Menu affiliation History title and history Item HeaderItem Historyheader = new HeaderItem (mmenutitles[0]);
  List.add (Historyheader); for (int i = 0; i < historytitles.length i++) {Eventitem HistoRyitem = new Eventitem (historytitles[i]);
  List.add (Historyitem);
  }//Menu add music title and music Item HeaderItem Musicheader = new HeaderItem (mmenutitles[1));
  List.add (Musicheader);
   for (int i = 0; i < musictitles.length i++) {Eventitem Musicitem = new Eventitem (musictitles[i));
  List.add (Musicitem);
  }//Menu add movie title and movie Item HeaderItem Movieheader = new HeaderItem (mmenutitles[2]);
  List.add (Movieheader);
   for (int i = 0; i < movietitles.length i++) {Eventitem Movieitem = new Eventitem (movietitles[i));
  List.add (Movieitem);
  } mylistadapter adapter = new Mylistadapter (this, list);

 Menulist.setadapter (adapter);

This data padding is a bit cumbersome. Customize the ListAdapter and then make the fit.
  Data   in Res/values/arrays.xml;

<?xml version= "1.0" encoding= "Utf-8"?> <resources> <string-array name= "History" > <item > Three countries ;/item> <item > Chu han </item> <item > Spring </item> <item > Warring States </item> </string-array > <string-array name= "Music" > <item > Jazz </item> <item > Classic </item> <item > Hyundai &L t;/item> <item > Ballads </item> </string-array> <string-array name= "movie" > <item > Suspense ;/item> <item > Love </item> <item > History </item> <item > Terror </item> </string-array > <string-array name= "title" > <item > History </item> <item > Music </item> <item > Movie ;/item> </string-array> <string-array name= "List_item" > <item > History </item> <item > Three Kingdoms & lt;/item> <item > Chu han </item> <item > Spring </item> <item > Warring States </item> <item > Music </item> <item > Jazz </item> <item > Classic </item> <item > Hyundai </item> <item > Ballad </item> <it Em > Movie </item> <item > Suspense </item> <item > Love </item> <item > History </item> <

 Item > Horror </item> </string-array> </resources>

Then there is the ListView listener:  

  private void Initlistener () {//Menu click event Listener menulist.setonitemclicklistener (New Draweritemclicklistener ()); }/* The Click Listner for ListView in the navigation drawer * * Private class Draweritemclicklistener implements Lis
    Tview.onitemclicklistener {@Override public void Onitemclick (adapterview<?> parent, view view, int position,
   Long id) {LOG.I ("Light", "Position:" + position);
  SelectItem (position);  } private void SelectItem (int position) {//update the main content by replacing fragments pagefragment fragment
  = new Pagefragment ();
  Passes the currently selected item to the fragment Bundle args = new Bundle ();
  Args.putint (Pagefragment.item_position_number, POSITION);

  Fragment.setarguments (args);
  Fragmenttransaction ft = MainActivity.this.getSupportFragmentManager (). BeginTransaction ();

  Ft.replace (R.id.content_frame, fragment). commit ();
  Drawlayout.closedrawer (menulist); Update selected item and title, then close the drawer MENULIST.SETITEMCHEcked (position, true);
 Note that the change here is the title of Actionbar Getactionbar (). Settitle (Listtitles[position]);

 }

What we care about is what happens when an item is clicked, which is the code:

private void SelectItem (int position) {
  //update the main content by replacing fragments
  pagefragment fragment = New Pagefragment ();
  Passes the currently selected item to the fragment
  Bundle args = new Bundle ();
  Args.putint (Pagefragment.item_position_number, POSITION);
  Fragment.setarguments (args);

  Fragmenttransaction ft = MainActivity.this.getSupportFragmentManager ()
    . BeginTransaction ();
  Ft.replace (R.id.content_frame, fragment). commit ();

  Drawlayout.closedrawer (menulist);
  Update selected item and title, then close the drawer
  menulist.setitemchecked (position, true);
  Note that the change here is the title of Actionbar
  Getactionbar (). Settitle (Listtitles[position]);
 

You can see from the code
1. First we get the content area first through the new Pagefragment ().
2. Package the data by bundle and inject it into fragment.setarguments (args) So fragment can get this data.
In the fragment class, through Getarguments (). GetInt (Item_position_number), you can get the values that are passed.
3. Then through Ft.replace (R.id.content_frame, fragment). commit (); Replace the content with the previously defined Pagefragment
4. Close menu via Drawlayout.closedrawer (menulist); In the entire code we only use the Drawlayout function
5. At the same time, change the title of Actionbar to SelectedItem corresponding value.
* Then someone will ask how we do not have ListView and drawerlayout to bind the operation. We've also said before that the second start in Drawerlayout is menu view, which is already bound inside.
These contents can be used to achieve the left and right side sliding menu effect.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.