概述
本篇博客是对developer.androID.com/上的Training课程的简单翻译,若是觉得翻译出来的理解有困难,请点击下方链接查看原文!
关于DrawerLayout的Training:@L_301_0@
关于DrawerLayout的API:http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
创建抽屉布局
创建一个抽屉布局必须得以DrawerLayout作为XML文件的根节点,记住,DrawerLayout引用的是AndroID.support.v4.DrawerLayout,然后在布局内部添加内容视图区域和一个或者两个抽屉视图区域,这里理解抽屉视图即为上面描述的菜单视图。例如下面一段布局中显示,在布局中添加一个FrameLayout作为内容区域(通常用来呈现Fragment),另外在下面定义了一个ListVIEw用来呈现抽屉菜单视图:
<androID.support.v4.Widget.DrawerLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/drawer_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <!-- The main content vIEw --> <FrameLayout androID:ID="@+ID/content_frame" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /> <!-- The navigation drawer --> <ListVIEw androID:ID="@+ID/left_drawer" androID:layout_wIDth="240dp" androID:layout_height="match_parent" androID:layout_gravity="start" androID:choiceMode="singleChoice" androID:divIDer="@androID:color/transparent" androID:divIDerHeight="0dp" androID:background="#111"/> </androID.support.v4.Widget.DrawerLayout>
这个布局文件示范了一些重要的布局特征.
初始化抽屉列表
在你的Activity中,第一件事就是初始化导航抽屉列表里面的元素,你如何做取决于你的应用程序的内容,但一个导航抽屉通常包括一个ListVIEw,因此清单应该由一个Adapter填充(例如ArrayAdapter或SimpleCursorAdapter).
例如,这里演示了如何用String array来初始化一个导航列表.
public class MainActivity extends Activity { private String[] mPlanetTitles; private DrawerLayout mDrawerLayout; private ListVIEw mDrawerList; ... @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mPlanetTitles = getResources().getStringArray(R.array.planets_array); mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); mDrawerList = (ListVIEw) findVIEwByID(R.ID.left_drawer); // Set the adapter for the List vIEw mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_List_item,mPlanetTitles)); // Set the List's click Listener mDrawerList.setonItemClickListener(new DrawerItemClickListener()); ... } }
这个代码调用setonItemClickListener()去接收导航抽屉列表的点击事件. 下一节将展示如何实现这个接口,当用户选择一个Item时改变内容视图.
处理导航点击事件
当用户选择了抽屉列表里面的一个Item时,系统调用onItemClickListener上的onItemClick(),给setonItemClickListener().
你在onItemClick()方法里面做什么,取决于你的app实现的结构. 在下面的例子中,选择每一个Item都会在主要内容的布局中插入一个不同的Fragment.
private class DrawerItemClickListener implements ListVIEw.OnItemClickListener { @OverrIDe public voID onItemClick(AdapterVIEw parent,VIEw vIEw,int position,long ID) { selectItem(position); } } /** Swaps fragments in the main content vIEw */ private voID selectItem(int position) { // Create a new fragment and specify the planet to show based on position Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); args.putInt(PlanetFragment.ARG_PLANET_NUMBER,position); fragment.setArguments(args); // Insert the fragment by replacing any existing fragment FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.ID.content_frame,fragment) .commit(); // Highlight the selected item,update the Title,and close the drawer mDrawerList.setItemChecked(position,true); setTitle(mPlanetTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } @OverrIDe public voID setTitle(CharSequence Title) { mTitle = Title; getActionbar().setTitle(mTitle); }
监听打开和关闭事件
侦听抽屉打开和关闭事件,调用你的DrawerLayout setDrawerListener(),并将其传递给DrawerLayout.DrawerListener的实现. 这个接口提供了回调抽屉事件,如onDrawerOpened()和onDrawerClosed ()。
然而,相对于实现DrawerLayout.DrawerListener,如果你的Activity包括工具栏,可以代替继承ActionbarDrawerToggle类. ActionbarDrawerToggle实现了DrawerLayout.DrawerListener. 所以你仍然可以覆盖这些回调,但它也有助于正确的交互行为,在工具栏的图标和导航抽屉之间(下一节将进一步讨论)。
就像在导航抽屉设计指南一样,当抽屉是可见的时候,你应该修改工具栏的内容,如改变标题和删除 *** 作Item. 下面的代码用ActionbarDrawerToggle类的一个实例,显示了如何重写DrawerLayout.DrawerListener的回调方法:
public class MainActivity extends Activity { private DrawerLayout mDrawerLayout; private ActionbarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; ... @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); ... mTitle = mDrawerTitle = getTitle(); mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); mDrawerToggle = new ActionbarDrawerToggle(this,mDrawerLayout,R.drawable.ic_drawer,R.string.drawer_open,R.string.drawer_close) { /** Called when a drawer has settled in a completely closed state. */ public voID onDrawerClosed(VIEw vIEw) { super.onDrawerClosed(vIEw); getActionbar().setTitle(mTitle); invalIDateOptionsMenu(); // creates call to onPrepareOptionsMenu() } /** Called when a drawer has settled in a completely open state. */ public voID onDrawerOpened(VIEw drawerVIEw) { super.onDrawerOpened(drawerVIEw); getActionbar().setTitle(mDrawerTitle); invalIDateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; // Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle); } /* Called whenever we call invalIDateOptionsMenu() */ @OverrIDe public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open,hIDe action items related to the content vIEw boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.ID.action_websearch).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } }
下一节描述ActionbarDrawerToggle构造函数参数和其他所需的步骤来设置它来处理工具栏的图标.
打开关闭应用图标
用户可以打开和关闭导航抽屉,通过手指从屏幕左侧的边缘滑动,但如果你使用工具栏,你应该也能允许用户打开和关闭它,通过触摸应用程序图标. 应用程序图标也可以显示一个特殊的图标关于导航抽屉的状态. 你可以实现所有这些行为通过使用ActionbarDrawerToggle,如前一节所示。
让ActionbarDrawerToggle工作,创建一个它的实例用它的构造方法,这就需要以下参数:
然后,无论你是否已经创建了一个ActionbarDrawerToggle的子类作为你的抽屉的Listener,你需要在几个Activity生命周期的地方,调用你的ActionbarDrawerToggle:
public class MainActivity extends Activity { private DrawerLayout mDrawerLayout; private ActionbarDrawerToggle mDrawerToggle; ... public voID onCreate(Bundle savedInstanceState) { ... 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); getActionbar().setdisplayHomeAsUpEnabled(true); getActionbar().setHomebuttonEnabled(true); } @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 onoptionsItemSelected(MenuItem item) { // Pass the event to ActionbarDrawerToggle,if it returns // true,then it has handled the app icon touch event if (mDrawerToggle.onoptionsItemSelected(item)) { return true; } // Handle your other action bar items... return super.onoptionsItemSelected(item); } ... }
官方Demo:http://xiazai.jb51.net/201701/yuanma/NavigationDrawer(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android组件创建DrawerLayout导航全部内容,希望文章能够帮你解决Android组件创建DrawerLayout导航所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)