不同活动的通用导航抽屉



我已经构建了一个包含导航抽屉活动的应用程序,但现在我希望它也出现在我的其他活动中。我不想在不同的班级中一次又一次地处理事件。

我也得到了一些建议,比如创建一个基类并扩展它,但由于我不能扩展多个类。

我还想使用片段,这样我就不必一次又一次地编写相同的代码我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"
    android:background="@color/colorPrimary">
    <include
        layout="@layout/app_bar_main"
        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>

nav_header.xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@drawable/bcet_logo" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/college_name"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/college_name_full" />
</LinearLayout>

MainActivity.java

package com.bcetapp.bcetbeta;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
        Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(null);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if (id == R.id.nav_student_list) {
            // Handle the camera action
        } else if (id == R.id.nav_staff_list) {
        } else if (id == R.id.nav_help) {
        } else if (id == R.id.nav_settings) {
        } else if (id == R.id.nav_action_share) {
        } else if (id == R.id.nav_contact_us) {
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    /****************************************************************************************USER DEFINED FUNCTIONS******************************************************************/
    public void viewMyProfile(View view){
        /*intent = new Intent(this, ProfileActivity.class);
        startActivity(intent);*/
    }
}

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_student_list"
            android:icon="@drawable/ic_group"
            android:title="Students" />
        <item
            android:id="@+id/nav_staff_list"
            android:icon="@drawable/ic_action_glasses"
            android:title=" College Staff" />
        <item
            android:id="@+id/nav_help"
            android:icon="@drawable/ic_action_help"
            android:title="help" />
        <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_menu_manage"
            android:title="Settings" />
    </group>
    <item android:title="More">
        <menu>
            <item
                android:title="About us"
                android:id="@+id/nav_about_us"
                android:icon="@drawable/ic_action_info"
                />
            <item
                android:id="@+id/nav_action_share"
                android:icon="@drawable/ic_action_share"
                android:title="Share App" />
            <item
                android:id="@+id/nav_contact_us"
                android:icon="@drawable/ic_menu_send"
                android:title="contact us" />
        </menu>
    </item>
</menu>

这并不容易。有 2 个技术滴答声来实现它。一个是创建使用片段,第二个是在所有活动中添加整个代码。

要在上述情况下获得结果,请在活动中添加片段并根据需要替换/添加活动

我也得到了一些建议,比如创建一个基类并扩展它,但由于我不能扩展多个类。

没错,但是关于类扩展的事情是,您可以创建一个扩展AppCompatActivity的基类,然后使您的活动扩展该基类:

public class BaseClass extends AppCompatActivity...
public class MainActivity extends BaseClass...

这将隐式地导致您的 MainActivity 扩展 AppCompatActivity。

相关内容

  • 没有找到相关文章

最新更新