Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1077205
  • 博文数量: 77
  • 博客积分: 821
  • 博客等级: 军士长
  • 技术积分: 1905
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-23 16:17
个人简介

学校:上海交通大学软件工程 学历:硕士 行业:从事流媒体移动开发 QQ: 412595942 邮箱:yiikai1987910@gmail.com

文章分类

全部博文(77)

文章存档

2016年(4)

2015年(15)

2014年(16)

2013年(12)

2012年(21)

2011年(9)

分类: Android平台

2015-04-29 16:40:07

    DrawLayout的实现官方文档上说的很详细了,但是在开发的过程中,发现还是有点问题,官方的文档有点旧了,mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);  这个方法原来的api可以设置app的icon图片,现在这个参数没有了。
  对于ActionBarDrawerToggle 这个类,作用时使得actionbar和drawlayout之间可以很好的相互组合。通过用ActionBarDrawerToggle可以实现app icon点击来打开抽屉的功能呢。
  主要需要实现:
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
   

点击(此处)折叠或打开

  1. @Override
  2.     public boolean onOptionsItemSelected(MenuItem item) {
  3.         // The action bar home/up action should open or close the drawer.
  4.         // ActionBarDrawerToggle will take care of this.
  5.         Log.v("haha",""+item.getItemId());
  6.         if (mDrawerToggle.onOptionsItemSelected(item)) {
  7.             return true;
  8.         }
  9.         // Handle action buttons
  10.         switch(item.getItemId()) {
  11.             case R.id.action_settings:

  12.                 return true;
  13.             default:
  14.                 return super.onOptionsItemSelected(item);
  15.         }
  16.     }
    已经在主activity中重载这个方法来是的对于actionbar的item的操作可以传给ActionBarDrawerToggle来处理,其实就我现在知道的就是处理actionbar的appicon的点击动作。
  


点击(此处)折叠或打开

  1. @Override
  2.     protected void onPostCreate(Bundle savedInstanceState) {
  3.         super.onPostCreate(savedInstanceState);
  4.         mDrawerToggle.syncState();
  5.     }
    这个方法的重载调用的syncState就是为了保持actionBar和ActionBarDrawerToggle的状态一致,这个函数是Activity的生命周期里的一个方法,只会调用一次。
  
  主要的代码:
  

点击(此处)折叠或打开

  1. package com.example.dolby.drawernavi;

  2. import android.app.ActionBar;
  3. import android.app.Fragment;
  4. import android.app.FragmentManager;
  5. import android.content.res.Configuration;
  6. import android.support.v4.widget.DrawerLayout;
  7. import android.support.v7.app.ActionBarActivity;
  8. import android.os.Bundle;
  9. import android.support.v7.app.ActionBarDrawerToggle;
  10. import android.util.Log;
  11. import android.view.Menu;
  12. import android.view.MenuInflater;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.AdapterView;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.Button;
  18. import android.widget.ListView;


  19. public class MainActivity extends ActionBarActivity {
  20.     private String[] mPlanetTitles = {"list1","list2","list3"};
  21.     private DrawerLayout mDrawerLayout;
  22.     private ListView mDrawerList;
  23.     private Button drawbtn;
  24.     private ActionBarDrawerToggle mDrawerToggle;
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_main);
  29.         //mPlanetTitles = getResources().getStringArray(R.array.planets_array);
  30.         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  31.         mDrawerList = (ListView) findViewById(R.id.left_drawer);
  32.         mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
  33.         // Set the drawer toggle as the DrawerListener
  34.         mDrawerLayout.setDrawerListener(mDrawerToggle);
  35.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  36.         getSupportActionBar().setHomeButtonEnabled(true);

  37.         // enable ActionBar app icon to behave as action to toggle nav drawer
  38.         //getActionBar().setDisplayHomeAsUpEnabled(true);
  39.         // Set the adapter for the list view
  40.         mDrawerList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1
  41.                 , mPlanetTitles));
  42.         // Set the list's click listener
  43.         mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
  44.     }

  45.     private class DrawerItemClickListener implements ListView.OnItemClickListener
  46.     {

  47.         @Override
  48.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  49.             selectItem(position);
  50.         }
  51.     }

  52.     private void selectItem(int position)
  53.     {
  54.         Fragment fragment = new PlanetFragment();

  55.         // Insert the fragment by replacing any existing fragment
  56.         FragmentManager fragmentManager = getFragmentManager();
  57.         fragmentManager.beginTransaction()
  58.                 .replace(R.id.content_frame, fragment)
  59.                 .commit();

  60.         // Highlight the selected item, update the title, and close the drawer
  61.         mDrawerList.setItemChecked(position, true);
  62.         setTitle(mPlanetTitles[position]);
  63.         mDrawerLayout.closeDrawer(mDrawerList);
  64.     }


  65.     @Override
  66.     protected void onPostCreate(Bundle savedInstanceState) {
  67.         super.onPostCreate(savedInstanceState);
  68.         mDrawerToggle.syncState();
  69.     }

  70. // @Override
  71. // public void onConfigurationChanged(Configuration newConfig) {
  72. // super.onConfigurationChanged(newConfig);
  73. // mDrawerToggle.onConfigurationChanged(newConfig);
  74. // }

  75. // @Override
  76. // public boolean onCreateOptionsMenu(Menu menu) {
  77. // MenuInflater inflater = getMenuInflater();
  78. // inflater.inflate(R.menu.menu_main,menu);
  79. // return super.onCreateOptionsMenu(menu);
  80. // }

  81. // @Override
  82. // public boolean onPrepareOptionsMenu(Menu menu) {
  83. //// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
  84. //// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
  85. //// return super.onPrepareOptionsMenu(menu);
  86. // return true;
  87. // }

  88.     @Override
  89.     public boolean onOptionsItemSelected(MenuItem item) {
  90.         // The action bar home/up action should open or close the drawer.
  91.         // ActionBarDrawerToggle will take care of this.
  92.         Log.v("haha",""+item.getItemId());
  93.         if (mDrawerToggle.onOptionsItemSelected(item)) {
  94.             return true;
  95.         }
  96.         // Handle action buttons
  97.         switch(item.getItemId()) {
  98.             case R.id.action_settings:

  99.                 return true;
  100.             default:
  101.                 return super.onOptionsItemSelected(item);
  102.         }
  103.     }


  104. }

点击(此处)折叠或打开

  1. <android.support.v4.widget.DrawerLayout
  2.     xmlns:android=""
  3.     android:id="@+id/drawer_layout"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent">
  6.     <!-- The main content view -->
  7.     <FrameLayout
  8.         android:id="@+id/content_frame"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="match_parent">
  11.         <!--<Button-->
  12.             <!--android:id="@+id/btn"-->
  13.             <!--android:layout_width="match_parent"-->
  14.             <!--android:layout_height="wrap_content"-->
  15.             <!--android:text="open"-->
  16.             <!--/>-->
  17.     </FrameLayout>

  18.     <!-- The navigation drawer -->
  19.     <ListView android:id="@+id/left_drawer"
  20.         android:layout_width="240dp"
  21.         android:layout_height="match_parent"
  22.         android:layout_gravity="start"
  23.         android:choiceMode="singleChoice"
  24.         android:divider="@android:color/transparent"
  25.         android:dividerHeight="0dp"
  26.         android:background="#FFFFFF"/>
  27. </android.support.v4.widget.DrawerLayout>





阅读(7782) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~