Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

android - inherit navigation drawer in all activities

I have a navigation drawer and now i want this navigation drawer to be in all my activities can anyone tell me how to inherit and use navigation drawer in all my activities

here is my code:

public  class NavigationActivity extends AppCompatActivity implements View.OnClickListener{

    protected DrawerLayout mDrawerLayout;

    protected FrameLayout mFrameLayout_ContentFrame;
    protected ActionBarDrawerToggle mActionBarDrawerToggle;
    protected Toolbar mToolbar;
    private LinearLayout mDrawerList2;

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

        mFrameLayout_ContentFrame = (FrameLayout) findViewById(R.id.main_activity_content_frame);
        mDrawerList2 = (LinearLayout) findViewById(R.id.left_drawer2);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);


        mDrawerLayout = (DrawerLayout) findViewById(R.id.main_activity_DrawerLayout);
        mDrawerLayout.closeDrawers();
        mDrawerLayout.setStatusBarBackground(R.color.colorPrimary);

        mActionBarDrawerToggle = new ActionBarDrawerToggle
                (
                        this,
                        mDrawerLayout,
                        mToolbar,
                        R.string.drawer_open,
                        R.string.drawer_close
                ) {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                // Disables the burger/arrow animation by default
                super.onDrawerSlide(drawerView, 0);
            }
        };

        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }
        mActionBarDrawerToggle.syncState();

    }


    @Override
    public void onClick(View v) {

    }
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList2);
        menu.findItem(R.id.action_Aboutus).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;


    }
    public boolean onOptionsItemSelected(MenuItem item){
        int items = item.getItemId();
        switch(items){

            case R.id.action_Settings:{
                Intent intent = new Intent(this,Settings.class);
                startActivity(intent);

            }break;

            case R.id.action_Contact_us:{
                Intent intent = new Intent(this,Contact.class);
                startActivity(intent);

            }break;

            case R.id.action_Aboutus:{
                Intent intent = new Intent(this,ChartStyle.class);
                startActivity(intent);

            }break;

            case R.id.action_Profile:{
                Intent intent = new Intent(this,ChartStyle.class);
                startActivity(intent);

            }break;
        }

        return super.onOptionsItemSelected(item);
    }


}

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_activity_DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <LinearLayout android:id="@+id/left_drawer2"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"

        android:background="#0F6177"/>

    <!-- The main content view -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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


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

    </LinearLayout>

    <!-- The navigation drawer -->


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

class where i want to inherit navigationdrawer :

public class Contact extends NavigationActivity implements View.OnClickListener {
    private EditText mName,mNumber,mEmail,mMessage;
    private FancyButton mSbmt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To inherit Navigation Drawer you need to use Fragments. Because we can use multiple fragments inside single activity. So you can not set another activity inside NavigationActivity . So use multiple fragments and call them from your NavigationActivity or from other fragments.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...