编程语言
首页 > 编程语言> > Java-Android:所有活动上的导航抽屉

Java-Android:所有活动上的导航抽屉

作者:互联网

我想在我的Android项目的所有活动上添加导航抽屉.这是MainActivity的代码:

public class MainActivity extends Activity {


        private String[] drawerListViewItems;
        private DrawerLayout drawerLayout;
        private ListView drawerListView;
        private ActionBarDrawerToggle actionBarDrawerToggle;

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

            // get list items from strings.xml
            drawerListViewItems = getResources().getStringArray(R.array.items);


            // get ListView defined in activity_main.xml
            drawerListView = (ListView) findViewById(R.id.left_drawer);

            // Set the adapter for the list view
            drawerListView.setAdapter(new ArrayAdapter<String>(this,
                    R.layout.drawer_listview_item, drawerListViewItems));

            // App Icon 
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            //drawerLayout = (DrawerLayout) findViewById(R.drawable.ic_drawer_2);

            actionBarDrawerToggle = new ActionBarDrawerToggle(
                    this,                  /* host Activity */
                    drawerLayout,         /* DrawerLayout object */
                    R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                    R.string.drawer_open,  /* "open drawer" description */
                    R.string.drawer_close  /* "close drawer" description */
                    );

            // Set actionBarDrawerToggle as the DrawerListener
            drawerLayout.setDrawerListener(actionBarDrawerToggle);

            getActionBar().setDisplayHomeAsUpEnabled(true); 

            // just styling option add shadow the right edge of the drawer
        drawerLayout.setDrawerShadow(R.drawable.ic_drawer, GravityCompat.START);

        drawerListView.setOnItemClickListener(new DrawerItemClickListener());
    }

    @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            actionBarDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            actionBarDrawerToggle.onConfigurationChanged(newConfig);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

             // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
            // then it has handled the app icon touch event
            if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        private class DrawerItemClickListener implements ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {

                    displayView(position);

                drawerLayout.closeDrawer(drawerListView);

            }

            private void displayView(int position) 
            {
                switch (position) 
                {
                case 0:
                    secondactivity();
                    break;


                case 1:
                    Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();
                    break;

                case 2:
                    Toast.makeText(MainActivity.this, "3", Toast.LENGTH_LONG).show();

                default:
                    break;
                }

            }
        }

        public void secondactivity (){

            Intent cambioActivity;

            cambioActivity = new Intent (this, SecondActivity.class);

            startActivity(cambioActivity);
        }
}

在此代码中,我创建了导航抽屉,我希望所有活动都具有导航抽屉,所以我的第二个活动的代码是这样的:

public class SecondActivity extends MainActivity {

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

    }

导航抽屉位于第一个活动上,但是没有其他活动,为什么?有人能帮我吗?

解决方法:

简单的方法是您应该创建片段.如果您准备为小事做准备,那么这就是给您的.它将使您在所有活动中都具有相同的导航抽屉.

创建抽屉_n_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

<YourDrawer
    android:id="@+id/drawer_drawer"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

</YourDrawer>

</RelativeLayout>

您的DrawerActivity.class

public class DrawerActivity extends Activity {

    public RelativeLayout fullLayout;
    public FrameLayout frameLayout;

    @Override
    public void setContentView(int layoutResID) {

        fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.drawer_n_activity, null);
        frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);

        getLayoutInflater().inflate(layoutResID, frameLayout, true);

        super.setContentView(fullLayout);

        //Your drawer content...

    }
}

现在,要在所有活动中包含相同的导航抽屉,并且还要注意一件事,所有活动都必须扩展DrawerActivity

public class MainActivity extends DrawerActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //layout for 1st activity
   }
}

public class SecondActivity extends DrawerActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity); //layout for 2nd activity
   }
}

标签:android-activity,navigation-drawer,java,android
来源: https://codeday.me/bug/20191120/2045905.html