How to create a navigation drawer on a fragment in android

时光毁灭记忆、已成空白 提交于 2019-12-13 00:43:29

问题


I need some suggestions in my design. I have an Activity class that loads multiple fragments. One of the fragment has Navigation drawer. When the user clicks any of the items in the navigation list it loads its fragments and has a back button to come back to the Fragment with the navigation Drawer. As shown in the image. A,B, C are items in navigation drawer which would replace the content and has a back button to go back to Fragment D with navigation drawer. I have seen multiple tutorials that load navigation drawer in activity and has navigation as main content. What I want is if the use clicks the icon on top it shows the list. Can it be part of the Fragment D layout ?

Any suggestions are appreciated?


回答1:


You will have to make a class which is having a navigation drawer in it and then you can extend the particular class wherever you want. if you want it on fragments just extend that particular activity holding those fragments with that class with navigation drawer.




回答2:


Use following code, activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FFF"
android:choiceMode="singleChoice"/>
</LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends FragmentActivity {
    final String[] data ={"one","two","three"};
    final String[] fragments ={
            "Your.package.Name.FragmentOne",
            "Your.package.Name.FragmentTwo",
            "Your.package.Name.FragmentThree"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         ArrayAdapter adapter = new ArrayAdapter(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

         final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
         final ListView navList = (ListView) findViewById(R.id.drawer);
         navList.setAdapter(adapter);
         navList.setOnItemClickListener(new OnItemClickListener(){
                 @Override
                 public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                         drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                                 @Override
                                 public void onDrawerClosed(View drawerView){
                                         super.onDrawerClosed(drawerView);
                                         FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                                         tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
                                         tx.commit();
                                 }
                         });
                         drawer.closeDrawer(navList);
                 }
         });
         FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
         tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
         tx.commit();
    }

}

And make your layouts for fragments in this case FragmentOne, FragmentTwo, FragmentThree




回答3:


You will have to make a class which is having a navigation drawer in it and then you can extend the particular class wherever you want .... if u want it on fragments then just extend that particular activity holding those fragments with that class with navigation drawer.

No,the activity on which you are replacing fragments should have navigation drawer.that is it.



来源:https://stackoverflow.com/questions/30332277/how-to-create-a-navigation-drawer-on-a-fragment-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!