How can I use the Navigation Drawer without fragments?

后端 未结 4 1058
不思量自难忘°
不思量自难忘° 2021-02-06 02:32

I\'m trying to follow this tutorial on how to create a Navigation Drawer, but I don\'t want to use Fragments to show new content after the user select an item from the drawer li

4条回答
  •  一生所求
    2021-02-06 03:06

    basic drawer without fragment

    package xxxxxx;
    
    
    import android.content.res.Configuration;
    import android.os.Bundle;
    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;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    
    
    
    
    public class loginhome extends AppCompatActivity {
    
        private ListView mDrawerList;
        private DrawerLayout mDrawerLayout;
        private ArrayAdapter mAdapter;
        private ActionBarDrawerToggle mDrawerToggle;
        private String mActivityTitle;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.loginhome);
            Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
            setSupportActionBar(topToolBar);
            mDrawerList = (ListView)findViewById(R.id.navList);
            mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
            mActivityTitle = getTitle().toString();
    
            addDrawerItems();
            setupDrawer();
    
    
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
    
        }
    
        private void addDrawerItems() {
            String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
            mAdapter = new ArrayAdapter(this, R.layout.drawer_items,R.id.label ,osArray);
            mDrawerList.setAdapter(mAdapter);
    
            mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    Toast.makeText(loginhome.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        private void setupDrawer() {
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
    
                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    getSupportActionBar().setTitle("Navigation!");
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
    
                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(mActivityTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
            };
    
            mDrawerToggle.setDrawerIndicatorEnabled(true);
            mDrawerLayout.setDrawerListener(mDrawerToggle);
        }
    
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            mDrawerToggle.syncState();
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }
    
        @Override
        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;
        }
    
        @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_help) {
                Toast.makeText(loginhome.this, "setting", Toast.LENGTH_LONG).show();
            }
            if(id == R.id.action_place){
                Toast.makeText(loginhome.this, "Refresh App", Toast.LENGTH_LONG).show();
            }
            if(id == R.id.action_search){
                Toast.makeText(loginhome.this, "Create Text", Toast.LENGTH_LONG).show();
            }
            // Activate the navigation drawer toggle
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    

    draweritems.xml

    
    
    
    
            
            
        
    

    toolbar.xml

    
    
        
    

提交回复
热议问题