Teach you to build a material design-style app (iv)

Source: Internet
Author: User

--next to the above.

3.3 Realization of navigation drawer menu item Selection

Although the navigation drawer has been implemented, you will find that the selection of the drawer list item is not responding because we have not implemented Recycleview items for click Monitoring.

Because we have 3 menusin the navigation drawer (Home,Friends & Messages), Therefore, you need to create a separate Fragment for each menu item .

in res-->layout , create a file named fragment_home.xml and add the following code.

Fragment_home.xml<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http ://schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:or        ientation= "vertical" tools:context= "androidhive.info.materialdesign.activity.HomeFragment" > <textview Android:id= "@+id/label" android:layout_alignparenttop= "true" android:layout_margintop= "100DP" Androi        D:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:gravity= "Center_horizontal" Android:textsize= "45DP" android:text= "HOME" android:textstyle= "bold"/> <textview android:l ayout_below= "@id/label" android:layout_centerinparent= "true" android:layout_width= "Fill_parent" Andro id:layout_height= "Wrap_content" android:textsize= "12DP" android:layout_margintop= "10DP" Android:gravi Ty= "Center_horizontal" android:text= "Edit fragment_home.xml to change the appearance"/> </RelativeLayout> 
(25) Under Activity package, create a class named Homefragment.java, and add the following code.

Homefragment.javaimport Android.app.activity;import Android.os.bundle;import android.support.v4.app.Fragment; Import Android.view.layoutinflater;import android.view.view;import android.view.ViewGroup;  public class Homefragment extends Fragment {public     homefragment () {        //Required Empty public constructor    }
    @Override public    void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);     }     @Override public    View Oncreateview (layoutinflater inflater, ViewGroup container,                             Bundle savedinstancestate) {        View Rootview = inflater.inflate (R.layout.fragment_home, container, false);          Inflate the layout for this fragment        return rootview;    }     @Override public    void Onattach (activity activity) {        Super.onattach (activity);    }     @Override public    void Ondetach () {        super.ondetach ();    }}

(26) Similarly, create Friendsfragment.java and Messagesfragment.java two fragment classes and create Fragment_friends.xml and Fragment_ Messages.xml layout file, please refer to the first two steps for the code.

Open the Mainactivity.java and make the following changes.

The >displayview () method displays the corresponding fragment View when the navigation menu is selected, which should be used when a navigation menu is selected, Called in ondraweritemselected () to show the corresponding view.

Mainactivity.javaimport Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmenttransaction;import Android.support.v4.widget.drawerlayout;import Android.support.v7.app.actionbaractivity;import Android.support.v7.widget.toolbar;import Android.view.menu;import Android.view.menuitem;import Android.view.View; Import Android.widget.Toast; public class Mainactivity extends Actionbaractivity implements Fragmentdrawer.fragmentdrawerlistener {private static     String TAG = MainActivity.class.getSimpleName ();    Private Toolbar Mtoolbar;     Private Fragmentdrawer drawerfragment;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);         Setcontentview (R.layout.activity_main);         Mtoolbar = (Toolbar) Findviewbyid (R.id.toolbar);        Setsupportactionbar (Mtoolbar);         Getsupportactionbar (). Setdisplayshowhomeenabled (True); Drawerfragment = (fragmentdrawer) Getsupportfragmentmanager (). Findfragmentbyid (R.id.fragment_navigation_drawer);        Drawerfragment.setup (R.id.fragment_navigation_drawer, (drawerlayout) Findviewbyid (R.id.drawer_layout), MToolbar);         Drawerfragment.setdrawerlistener (this);    Display the first navigation drawer view on app launch Displayview (0); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Act        Ion Bar if it is present.        Getmenuinflater (). Inflate (R.menu.menu_main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.         int id = item.getitemid (); Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true; } if (id = = R.id.action_search) {Toast.maketext (Getapplicationcontext (), "Search action is selected!", T Oast.            Length_short). Show ();        return true;    } return super.onoptionsitemselected (item);    } @Override public void ondraweritemselected (view view, int position) {Displayview (position);        } private void Displayview (int position) {Fragment Fragment = null;        String title = getString (R.string.app_name);                Switch (position) {Case 0:fragment = new homefragment ();                title = GetString (R.string.title_home);            Break                Case 1:fragment = new friendsfragment ();                title = GetString (R.string.title_friends);            Break                Case 2:fragment = new messagesfragment ();                title = GetString (r.string.title_messages);            Break DefauLt:break;            } if (fragment! = null) {Fragmentmanager Fragmentmanager = Getsupportfragmentmanager ();            Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();            Fragmenttransaction.replace (r.id.container_body, fragment);             Fragmenttransaction.commit ();        Set the toolbar title Getsupportactionbar (). Settitle (title); }    }}

Well, running the app again, you can see that the Select navigation menu works, and clicking will display the corresponding view below toolbar.




Complete the full text.

Original Address: http://www.androidhive.info/2015/04/android-getting-started-with-material-design/


Teach you to build a material design-style app (iv)

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.