简体   繁体   English

多个活动中的导航抽屉

[英]Navigation drawer in multiple activities

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 . 使用Tamada教程,我创建了一个NavigaionActivity和多个要替换在FrameLayout fragments 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. 在该活动中使用显示Navigation viewxml代码。 (DrawerLayout, CoordinatorLayout, AppBarLayout etc) (DrawerLayout,CoordinatorLayout,AppBarLayout等)

    Then in Activity.java, on click of menu item diverting to the Navigation Activity. 然后,在Activity.java中,单击转向“导航活动”的菜单项。

Code : 代码

XML: 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: 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. 并处理对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? 还有其他方法吗?

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 创建一个xml这将有文件DrawerLayoutNavigationView (可以使用在问题中给出的XML,而主要内容) - navigation.xml
  2. As suggested in many answers "create a BaseActivity which extends AppCompatActivity. And inflate navigation.xml . 正如许多答案中所建议的那样,“创建extends AppCompatActivity的BaseActivity。并为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. 在任何一个Activity ,你想用这个NavigationMenu ,延长BaseActivity该类。

     GraphActivity extends BaseActivity { .... } 
  4. In GraphActivity.xml add the NavigationMenu code. GraphActivity.xml添加NavigationMenu代码。 You can't just include the navigation.xml it will disable the current xml widgets. 您不能只包含navigation.xml它会禁用当前的xml小部件。

  5. Done! 做完了!

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);
    }

Plz Don't use it this way 请不要用这种方式

Instead use Google recommended MasterDetailFlow Design 而是使用Google推荐的MasterDetailFlow设计

这是MasterDetailFlow

You can read how to implement this on 您可以阅读如何实施

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

NavigationDrawer令人垂涎。...使用其他东西因为使用起来不太舒服。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM