定制导航抽屉带活动&分段

时间:2015-04-23 07:22:49

标签: android android-fragments android-activity

这是主要活动的代码,其中包含带有三个按钮的横向菜单,我想要做的是:

修改此代码并替换" listener"中的操作。横向菜单中的按钮,现在当我按下按钮时,会创建一个新片段,但我想创建一个新活动(不是片段),维护布局(横向菜单)。 我怎么能这样做?

    public class MainActivity extends Activity {

        // declare properties
        private String[] mNavigationDrawerItemTitles;
        private DrawerLayout mDrawerLayout;
        private ListView mDrawerList;

        private ActionBarDrawerToggle mDrawerToggle;

        // nav drawer title
        private CharSequence mDrawerTitle;

        // used to store app title
        private CharSequence mTitle;

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

            // for proper titles
            mTitle = mDrawerTitle = getTitle();

            // initialize properties
            mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.left_drawer);

            // list the drawer items
            ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];

            drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_action_copy, "Create");
            drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_action_refresh, "Read");
            drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_share, "Help");

            // Pass the folderData to our ListView adapter
            DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);

            // Set the adapter for the list view
            mDrawerList.setAdapter(adapter);

            // set the item click listener
            mDrawerList.setOnItemClickListener(new DrawerItemClickListener());





            // for app icon control for nav drawer
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerToggle = new ActionBarDrawerToggle(
                    this,                  /* host Activity */
                    mDrawerLayout,         /* 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 */
                    ) {

                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getActionBar().setTitle(mTitle);
                }

                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    getActionBar().setTitle(mDrawerTitle);
                }
            };

            // Set the drawer toggle as the DrawerListener
            mDrawerLayout.setDrawerListener(mDrawerToggle);

            // enable ActionBar app icon to behave as action to toggle nav drawer
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true);



            if (savedInstanceState == null) {
                // on first time display view for first nav item
                selectItem(0);
            }
        }

        @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) {

            // The action bar home/up action should open or close the drawer.
            // ActionBarDrawerToggle will take care of this.
           if (mDrawerToggle.onOptionsItemSelected(item)) {
               return true;
           }

           return super.onOptionsItemSelected(item);
        }

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


        // navigation drawer click listener
        private class DrawerItemClickListener implements ListView.OnItemClickListener {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }

        }

        private void selectItem(int position) {

            // update the main content by replacing fragments

            Fragment fragment = null;

            switch (position) {
            case 0:
                fragment = new CreateFragment();
                break;
            case 1:
                fragment = new ReadFragment();
                break;
            case 2:
                fragment = new HelpFragment();
                break;

            default:
                break;
            }

            if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

                // update selected item and title, then close the drawer
                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                setTitle(mNavigationDrawerItemTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);

            } else {
                // error in creating fragment
                Log.e("MainActivity", "Error in creating fragment");
            }
        }

        @Override
        public void setTitle(CharSequence title) {
            mTitle = title;
            getActionBar().setTitle(mTitle);
        }
    }

***********编辑1 ***********

我尝试这种方法:

我替换了这部分:

private void selectItem(int position) {  
  // update the main content by replacing fragments  
  Fragment fragment = null;

  switch (position) {
   case 0:
   fragment = new CreateFragment();
   break;
   case 1:
   fragment = new ReadFragment();
   break;
   case 2:
   fragment = new HelpFragment();
   break;

   default:
   break;
  }

  if (fragment != null) {
   FragmentManager fragmentManager = getFragmentManager();
   fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

   // update selected item and title, then close the drawer
   mDrawerList.setItemChecked(position, true);
   mDrawerList.setSelection(position);
   setTitle(mNavigationDrawerItemTitles[position]);
   mDrawerLayout.closeDrawer(mDrawerList);

  } else {
   // error in creating fragment
   Log.e("MainActivity", "Error in creating fragment");
  }
 }

用这个:

private void selectItem(int position) {  
      // update the main content by replacing fragments  
      Fragment fragment = null;

      switch (position) {
       case 0:

       break;
       case 1:
        Intent intent = new Intent(this, MyCustomActivity.class);
        startActivity(intent)
       break;
       case 2:
       Intent intent2 = new Intent(this, MyCustomActivity.class);
       startActivity(intent2)
       break;

       default:
       break;
      }

   // update selected item and title, then close the drawer
   mDrawerList.setItemChecked(position, true);
   mDrawerList.setSelection(position);
   setTitle(mNavigationDrawerItemTitles[position]);
   mDrawerLayout.closeDrawer(mDrawerList);
     }

但是当我尝试使用我的设备时,该按钮并不像我没有绑定动作一样工作

***********编辑2 ***********

    public class MainActivity extends Activity implements View.OnClickListener {

        // declare properties
        private String[] mNavigationDrawerItemTitles;
        private DrawerLayout mDrawerLayout;
        private ListView mDrawerList;

        private ActionBarDrawerToggle mDrawerToggle;

        // nav drawer title
        private CharSequence mDrawerTitle;

        // used to store app title
        private CharSequence mTitle;

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

            // for proper titles
            mTitle = mDrawerTitle = getTitle();

            // initialize properties
            mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.left_drawer);

            // list the drawer items
            ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];

            drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_action_copy, "Create");
            drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_action_refresh, "Read");
            drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_share, "Help");

            // Pass the folderData to our ListView adapter
            DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);

            // Set the adapter for the list view
            mDrawerList.setAdapter(adapter);

            // set the item click listener
            mDrawerList.setOnItemClickListener(new DrawerItemClickListener());





            // for app icon control for nav drawer
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerToggle = new ActionBarDrawerToggle(
                    this,                  /* host Activity */
                    mDrawerLayout,         /* 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 */
                    ) {

                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getActionBar().setTitle(mTitle);
                }

                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    getActionBar().setTitle(mDrawerTitle);
                }
            };

            // Set the drawer toggle as the DrawerListener
            mDrawerLayout.setDrawerListener(mDrawerToggle);

            // enable ActionBar app icon to behave as action to toggle nav drawer
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true);



            if (savedInstanceState == null) {
                // on first time display view for first nav item
                selectItem(0);
            }
        }

        @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) {

            // The action bar home/up action should open or close the drawer.
            // ActionBarDrawerToggle will take care of this.
           if (mDrawerToggle.onOptionsItemSelected(item)) {
               return true;
           }

           return super.onOptionsItemSelected(item);
        }

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


        // navigation drawer click listener
        private class DrawerItemClickListener implements ListView.OnItemClickListener {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }

        }

        private void selectItem(int position) {

                // update selected item and title, then close the drawer
                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                setTitle(mNavigationDrawerItemTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);

        }

        @Override
        public void setTitle(CharSequence title) {
            mTitle = title;
            getActionBar().setTitle(mTitle);
        }

@Override
public void onClick(View arg0) {
    System.out.println("DEBUG"+arg0);
//ACTION, arg0 no value 
}

        }

2 个答案:

答案 0 :(得分:1)

创建新的活动扩展具有导航抽屉的 MainActivity 。此新活动将自动拥有导航抽屉。

检查此答案以获取更多详细信息:

Same Navigation Drawer in different Activities

答案 1 :(得分:0)

你是否试图在private void selectItem(int position) { ... }中尝试充气而不是创建新的片段我的意思是,当你获得该位置时,为当前活动创建一个Intent Intent intent = new Intent(this, YourCustomActivity.class);并导航到它与startActivity(intent)