【问题标题】:How to add the Navigation Drawer to Multiple Activites如何将导航抽屉添加到多个活动
【发布时间】:2018-06-07 21:45:37
【问题描述】:

我有一个应用程序,通过查看 android studio 提供的示例,我在其中添加了 Navigation Drawer。但现在我想将抽屉添加到其他活动中。为此,我使用 Android Studio 提供的Navigation Drawer Activity 创建了一个新项目。我发现实现这一目标的最简单方法是 制作一个BaseActivity 并从BaseActivity 扩展我所有需要Navigation Drawer 的其他活动这是我的一些代码。

BaseActivity.java

package com.example.eddie.drawerapp2;
//I did not include the imports to make this shorter

public class BaseActivity extends AppCompatActivity implements
    NavigationView.OnNavigationItemSelectedListener
{
protected FrameLayout frameLayout;
private DrawerLayout drawer;
private NavigationView navView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.base_activity);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    android.support.v7.app.ActionBarDrawerToggle toggle = new android.support.v7.app.ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    frameLayout = (FrameLayout)findViewById(R.id.content_frame);
    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    navView = (NavigationView)findViewById(R.id.nav_view);

    navView.setNavigationItemSelectedListener(this);


}

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

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } 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) {

    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

activity_base.xml

<?xml version="1.0" encoding="utf-8"?
<android.support.v4.widget.DrawerLayout 
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

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

<android.support.design.widget.NavigationView
     android:id="@+id/nav_view"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_gravity="start"
     android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

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

当我转到activity_base.xml 并将其更改为设计视图时,它看起来很好。

MainActivity.java

package com.example.eddie.drawerapp2;

//skip imports

public class MainActivity extends BaseActivity{

    private DrawerLayout drawer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //inflate your activity layout here!
        drawer = (findViewById(R.id.drawer_layout));
        View contentView = inflater.inflate(R.layout.activity_main, null, false);
        drawer.addView(contentView, 0);

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

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

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

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    tools:context="com.example.eddie.drawerapp2.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

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

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.eddie.drawerapp2.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

activity_main.xmlMainActivity.java 有逻辑错误。它只显示内容和一个空白工具栏。滑动打开NavigationDrawer 也无济于事。我真的不知道该怎么办,这只是我的尝试。请帮帮我,我真的卡住了。我需要做什么才能将NavigationDrawer 添加到MainActivity 以及其他活动?

【问题讨论】:

标签: java android navigation-drawer


【解决方案1】:

我以前做过,我认为您在 BaseActivity.java 的代码中缺少的是覆盖 setContentView 方法,以便可以将内容视图设置到框架布局中,从而创建其他活动的行为,像子类一样实现。

1。 BaseActivity.java

package com.example.eddie.drawerapp2;
//I did not include the imports to make this shorter

public class BaseActivity extends AppCompatActivity implements
    NavigationView.OnNavigationItemSelectedListener
{
protected FrameLayout frameLayout;
private DrawerLayout drawer;
private NavigationView navView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.base_activity);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    android.support.v7.app.ActionBarDrawerToggle toggle = new android.support.v7.app.ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    frameLayout = (FrameLayout)findViewById(R.id.content_frame);
    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    navView = (NavigationView)findViewById(R.id.nav_view);

    navView.setNavigationItemSelectedListener(this);


}

     /* Overriding all setContentView methods to put the content view to the FrameLayout frameLayout
  * so that, we can make other activity implementations looks like normal activity subclasses.
  */

@Override
public void setContentView(int layoutResID) {
    if (frameLayout != null) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        View stubView = inflater.inflate(layoutResID, frameLayout, false);
        frameLayout.addView(stubView, lp);
    }
}

@Override
public void setContentView(View view) {
    if (frameLayout != null) {
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        frameLayout.addView(view, lp);
    }
}

@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
    if (frameLayout != null) {
        frameLayout.addView(view, params);
    }
}



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

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } 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) {

    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}
  1. 您的 MainActivity.java 应该如下所示 MainActivity.java

    package com.example.eddie.drawerapp2;
    
    //skip imports
    
    public class MainActivity extends BaseActivity{
    
        private DrawerLayout drawer;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        }
     }
    

3。 activity_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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


    </RelativeLayout>

【讨论】:

  • 感谢您的回答!对此,我真的非常感激。有一件事,在每个setContentView 方法的末尾需要调用super.setContentView。这使应用程序运行,但它不会使抽屉工作。带有toolbar 的所有内容都必须从baseActivity 移动到MainActivity.onCreate()。以及NavigationView 也必须从activity_base 复制到activity_main
  • 这很有趣,因为这在我的案例中从未发生过,尤其是在导航视图中。就我而言,我完全删除了工具栏。但我认为您可能不得不在 BaseActivity.java 中对这些内容进行一些覆盖
  • 我在您提供的示例中所做的一切都像您提供的那样,它兑现说尝试调用虚拟方法'void android.support.v4.widget.DrawerLayout.addDrawerListener(android.support.v4.widget.DrawerLayout $DrawerListener)' 在一个空对象引用上,并在 BaseActivity 中指向这个 drawer.addDrawerListener(toggle); 你能帮忙吗
  • ok 将该项目的代码上传到github并分享链接。我不介意,因为它严格用于教育目的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-02
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
相关资源
最近更新 更多