Android studio navigation drawer with fragment or activities

独自空忆成欢 提交于 2019-11-30 14:14:22

问题


I'm trying to develop an application using the Navigation drawer template of Android Studio. So, I created a new project using this template. But when I run the program and click on a menu item, the view doesn't change. I searched everywhere on internet, but I didn't see how I can handle this.

This is the code provided by Android Studio:

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    int id = item.getItemId();

    if (id == R.id.nav_camara) {


    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

What I want to achieve is to replace the current view with the appropriate view for the menu item clicked.

Is Fragment the best way to do this or should I create a different activity for each menu item?


回答1:


In your case adding fragment will be the best solution.

create a fragment BlankFragment.java

public class BlankFragment extends Fragment {

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

}

and create fragment_black.xml

    <FrameLayout 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"
    tools:context="com.above_inc.shyam.drawer.BlankFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

now replace your method

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    Fragment fragment = null;
    if (id == R.id.nav_camera) {
        // Handle the camera action
        fragment = new BlankFragment();
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;

}

add below code in your content_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.above_inc.shyam.drawer.MainActivity"
    tools:showIn="@layout/app_bar_main">


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

you can also add more fragment to other options same as camera in above code




回答2:


Fragment will be the best way if you want your navigation drawer to be shown on the next screen when you click on the menu item.

If you will use activity then navigation drawer will not be shown unless you make a BaseActivity which extends Navigation Drawer and use everywhere. In this case you have to change acitivty transition animation too as new activity will pop up after clicking on the menu which may look odd.




回答3:


Right click on your project -> New -> Fragment.

Edit the switch case accordingly.




回答4:


@Passiondroid's answer on using Fragments only if you want Navigation drawer across all activities is spot on.

About starting an activity on click of the menu item, in one of my apps, I follow this,

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        switch(id)
        {

            case R.id.nav_profile:
            selection = 1;
                intent = new Intent("android.intent.action.WEB");
                intent.putExtra("selection",selection);
                startActivity(intent);
                break;
            case R.id.nav_books:
                intent = new Intent("android.intent.action.WEBD");
                selection = 2;
                intent.putExtra("selection",selection);
                startActivity(intent);
                break;
            case R.id.fav_quotes:
                intent = new Intent ("android.intent.action.SHOWFAVTES");
                selection = 3;
                intent.putExtra("selection",selection);
                startActivity(intent);
                break;
             default:
                break;
        }

To change and configure the Menu Item, you can find the "activity_main_drawer.xml" under res->Menu.

And on click of each of the menu items, you can start an activity as per your wish. You have declare the activities in your AndroidManifest.xml file before you use it.



来源:https://stackoverflow.com/questions/36079028/android-studio-navigation-drawer-with-fragment-or-activities

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