Navigation drawer in multiple activities

て烟熏妆下的殇ゞ 提交于 2019-12-23 04:26:39

问题


I know this question has been asked multiple times. But my concern is different.

Using Mr. Tamada Tutorial I've created a NavigaionActivity and multiple fragments to replace in FrameLayout. It's working fine.

Now, after selecting any option in a fragment, another activity gets open.

I want the same Navigation menu in that Activity.

Navigation view -- fragments -- Activity (display navigation view here)

What I tried:

  1. use the xml code of displaying Navigation view in that activity. (DrawerLayout, CoordinatorLayout, AppBarLayout etc)

    Then in Activity.java, on click of menu item diverting to the Navigation Activity.

Code:

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout  
    ....> 

    <android.support.design.widget.CoordinatorLayout  
    ....> 

         <android.support.design.widget.AppBarLayout
         ....> 

             <android.support.v7.widget.Toolbar
             .... /> 

         </android.support.design.widget.AppBarLayout>

         <LinearLayout
         .... /> <!-- main content of this Acitivity-->

    </android.support.design.widget.CoordinatorLayout>  

    <android.support.design.widget.NavigationView
      .... /> 
</android.support.v4.widget.DrawerLayout>

Activity.java:

public void dashboard(MenuItem item) {
    Bundle bundle = new Bundle();
    bundle.putString("fragment", Constant.DASHBOARD_FRAGMENT);
    UtilityClass.newActivity(this, NavigationActivity.class, bundle);
}

And handling the call on Navigation Activity. It is doing the task but code isn't re-usable

  1. Create a separate layout file for Navigation and include it in the Activity. But, this is replacing the main content. Here only included Navigation is visible.

Is there any other way to do it?


回答1:


Downvoters - here the solution is..

  1. Create an xml file which will have DrawerLayout and NavigationView (one can use the xml given in Question, without the main content) - navigation.xml
  2. As suggested in many answers "create a BaseActivity which extends AppCompatActivity. And inflate navigation.xml.

    public class BaseActivity extends AppCompatActivity {  
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {  
    
            super.onCreate(savedInstanceState);
            View view = View.inflate(this, R.layout.navigation, null);
    
            // view declarations
            DrawerLayout drawer = (DrawerLayout) view.findViewById(R.id.drawer_layout);
            NavigationView navigationView = (NavigationView) view.findViewById(R.id.nav_view);
            ...... }}
    
  3. In whichever Activity you wanna use this NavigationMenu, extend BaseActivity for that class.

    GraphActivity extends BaseActivity { .... }
    
  4. In GraphActivity.xml add the NavigationMenu code. You can't just include the navigation.xml it will disable the current xml widgets.

  5. Done!




回答2:


Try this:

public class MainActivity extends BaseActivity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_describtion);
        getLayoutInflater().inflate(R.layout.activity_main, frameLayout);
        mDrawerList.setItemChecked(position, true);
    }



回答3:


Plz Don't use it this way

Instead use Google recommended MasterDetailFlow Design

You can read how to implement this on

https://inducesmile.com/android/android-fragment-masterdetail-flow-tutorial-in-android-studio/




回答4:


NavigationDrawer is depricated....use something else for it is not much comfortable to use.



来源:https://stackoverflow.com/questions/43061284/navigation-drawer-in-multiple-activities

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