多个活动中的导航抽屉 [英] Navigation drawer in multiple activities

查看:106
本文介绍了多个活动中的导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过多次了.但是我的担心是不同的.

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

使用先生. Tamada教程我创建了一个NavigaionActivity和多个fragments来替换FrameLayout.一切正常.

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.

我要在该活动中使用相同的导航菜单.

导航视图-片段-活动(在此处显示导航视图)

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

我尝试过的事情:

  1. 在该活动中使用显示Navigation viewxml代码. (DrawerLayout,CoordinatorLayout,AppBarLayout等)

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

然后在Activity.java中,单击转向导航活动的菜单项.

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

代码:

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. It is doing the task but code isn't re-usable

  1. 为导航创建一个单独的布局文件,并将其包含在活动"中.但是,这取代了主要内容.在此仅包含导航是可见的.

还有其他方法吗?

推荐答案

下降投票者-解决方案是..

Downvoters - here the solution is..

  1. 创建一个包含DrawerLayoutNavigationViewxml文件(一个人可以使用Question中给出的xml,而没有主要内容)-navigation.xml
  2. 如许多答案中所建议,创建一个extends AppCompatActivity的BaseActivity.然后对navigation.xml进行充气.

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

  • 要使用此NavigationMenu的任何Activity,请为该类扩展BaseActivity.

  • In whichever Activity you wanna use this NavigationMenu, extend BaseActivity for that class.

    GraphActivity extends BaseActivity { .... }
    

  • GraphActivity.xml中添加NavigationMenu代码.您不能只包含navigation.xml它会禁用当前的xml小部件.

  • In GraphActivity.xml add the NavigationMenu code. You can't just include the navigation.xml it will disable the current xml widgets.

    完成!

    这篇关于多个活动中的导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆