MasterofProject

Android sliding menu DrawerLayout

Label Android ApplicationSliding menu
291 people read comment(1) Collection Report
Classification:

The sliding menu control DrawerLayoutyesSupport LibraryThe package implements the sliding menu effect control, perhapsBecause the third party controls such asMenuDrawerAfter the appearance of the,GoogleProduct of reference. DRawerLayoutIt is divided into two parts, the side menu and the main content, and the side menu can be unfolded and hidden according to the gesture.DrawerLayoutOwn characteristics), the contents of the main content area can be changed with the click of the menu, the content will be to achieve their own.

The following example is mainly based on the official document transplant, a simple change:

The interface layout and ----------------------------------------


Xmlns:android= http://schemas.android.com/apk/res/android "<android.support.v4.widget.DrawerLayout"
@+id/test_drawer_layout "android:id="
Match_parent "android:layout_width="
Match_parent "android:layout_height=" >

<FrameLayout
@+id/content_fr_layout "android:id="
Match_parent "android:layout_width="
Android:layout_height= "match_parent" / >

<ListView
@+id/left_drawer_lv "android:id="
200dp "android:layout_width="
Match_parent "android:layout_height="
Start "android:layout_gravity="
#111 "android:background="
SingleChoice "android:choiceMode="
@android:color/transparent "android:divider="
Android:dividerHeight= "1DP" / >

</android.support.v4.widget.DrawerLayout>

Layout description:

1, create a Layout Drawer in the need to drawer menu interface, using DrawerLayout as the interface root control.
2, in the DrawerLayout inside the first View for the current main contents of the interface; second and third View for the drawer menu. If the current interface only needs a drawer menu, then third View can be omitted.
3, the following example of DrawerLayout which contains two View, the first FrameLayout is the current interface of the main contents of the display area; second ListView for the drawer menu.

Part of the code ------------------------------------ employed

Class TestDrawerActivity extends Activity public {
Drawer menu (as the root / * * * / layout)
DrawerLayout mDrawerLayout private;
Drawer menu menu on the left: / * * * /
ListView mDrawerList private;
/ * * drawer menu: application icon drawer switch.
ActionBarDrawerToggle mDrawerToggle private;
Drawer title / * * * /
CharSequence mDrawerTitle private;
Drawer menu menu on the left: / * * * / data
String[] mLeftTitles private;


@Override
Void onCreate protected (savedInstanceState Bundle) {
Super.onCreate (savedInstanceState);
SetContentView (R.layout.activity_test_drawer);
FindViewById ();
SetListeners ();
Initdata ();
If (savedInstanceState = = null) {
SelectItem (0);
}
}


Void findViewById private () {
MDrawerLayout = (DrawerLayout) findViewById (R.id.test_drawer_layout);
MDrawerList = (ListView) findViewById (R.id.left_drawer_lv);
MDrawerToggle = ActionBarDrawerToggle new (this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
Close operation menu / * * * /
Void onDrawerClosed public (view View) {
GetActionBar ().SetTitle (mDrawerTitle);
InvalidateOptionsMenu ();
}


Open the menu / * * * /
Void onDrawerOpened public (drawerView View) {
GetActionBar ().SetTitle (mDrawerTitle);
InvalidateOptionsMenu ();
}
};
}


Void setListeners private () {
MDrawerList.setOnItemClickListener (DrawerItemClickListener new) ();
MDrawerLayout.setDrawerListener (mDrawerToggle);
}


Void initdata private () {
* / / * * menu title
MDrawerTitle = getTitle ();
MLeftTitles =.GetStringArray () getResources (R.array.planets_array);
/ * *设置拉出导航菜单时阴影,官方示例不明显,可把图片背景修改一下*/
mdrawerlayout。setdrawershadow(r.drawable.drawer_shadow,gravitycompat。开始);
mdrawerlist。setadapter(新ArrayAdapter <字符串>(这r.layout.drawer_list_item,mlefttitles));
/ * * /操作模式开启ActionBar
getactionbar() setdisplayhomeasupenabled(假);
getactionbar() sethomebuttonenabled(假);
}


“重写”
公共布尔onCreateOptionsMenu(菜单){
menuinflater气筒= getmenuinflater();
打气筒充气(r.menu.main,菜单);
返回超级onCreateOptionsMenu(菜单);
}


“重写”
公共布尔onprepareoptionsmenu(菜单){
布尔draweropen = mdrawerlayout isdraweropen(mdrawerlist);
菜单。finditem(r.id.action_websearch setVisible()!draweropen);
收益超onprepareoptionsmenu(菜单);
}


“重写”
公共布尔onOptionsItemSelected(菜单项){
如果(mdrawertoggle。onOptionsItemSelected(项)){ return true;}
开关(项目。getitemid()){
案例r.id.action_websearch:
意图意图=新的意图(intent。action_web_search);
意图。putExtra(searchmanager.query,getactionbar()。gettitle());
如果(意图。resolveactivity(getpackagemanager())!=空){
startActivity(意图);
}
否则{
烤面包。maketext(这r.string.app_not_available,干杯。length_long show());
}
返回真;
违约:
返回超级onOptionsItemSelected(项目);
}
}


/ * * /菜单列表的点击操作
私有类draweritemclicklistener实现listview.onitemclicklistener {
“重写”
公共无效onitemclick(adapterview <?>母公司观,int的位置,长的ID){
选择项(位置);
}
}


private void选择项(int的位置){
破片=新planetfragment();
束参数=新bundle();
一putint(planetfragment.arg_planet_number,位置);
setarguments(args)片段;


getfragmentmanager() FragmentManager FragmentManager =;
FragmentManager。begintransaction()。取代(r.id.content_fr_layout,commit()片段);
mdrawerlist SetItemChecked(位置,真的);
settitle(mlefttitles [位置]);
mdrawerlayout closedrawer(mdrawerlist);
}


“重写”
公共无效settitle(CharSequence标题){
mdrawertitle =标题;
getactionbar() settitle(mdrawertitle);
}


“重写”
protected void onpostcreate(束savedinstancestate){
超级onpostcreate(savedinstancestate);
mdrawertoggle。syncstate();
}


“重写”
公共无效onconfigurationchanged(配置newconfig){
超级onconfigurationchanged(newconfig);
mdrawertoggle onconfigurationchanged(newconfig);
}


公共静态类planetfragment延伸片段{
public static final String arg_planet_number =“planet_number”;


公共planetfragment() {
}


“重写”
公众视野oncreateview(LayoutInflater inflater,ViewGroup容器,束savedinstancestate){
查看RootView =充气。充气(r.layout.fragment_planet,容器,假);
int i = getarguments() getInt(arg_planet_number);
字符串的星球= getresources()。getstringarray(r.array。planets_array)[我];


int imageid = getresources()。getidentifier(行星。toLowerCase(现场。getdefault()),“冲”,getactivity()。getpackagename());
((ImageView)RootView。findViewById(r.id.image))。setimageresource(imageid);
getactivity() settitle(星球);
返回RootView;
}
}
}
Android支持。V4。安使用时注意事项部件:

事项1:显示界面主要内容的视图(上面的FrameLayout)必须为安的第一个子看来,国为XML布局文件中的观顺序为Android系统中的z顺序,而抽屉必须出现在内容之上。显示界面内容的观宽度和高度设置为和父观一样,原因在于当抽屉菜单不可见的时候,界面内容代表整个界面UI。

事项2:抽屉菜单 (ListView)必须使用Android:layout_gravity属性设置水平的重力值。如果要支持右至左(RTL,从右向左阅读)语言用“开始”代替“左”(当在RTL语言运行时候,菜单出现在右侧)。抽屉菜单的宽度为 DP单位而高度和父一样抽屉菜单的宽度建议不超过320dp,这样用户可以在菜单打开的时候看到部分内容界面视图。

Item 3: if you need to listen to the menu to turn off the event, you need to call the DrawerLayout class setDrawerListener () function, the parameters for the DrawerLayout.DrawerListener interface. This interface provides a callback function that opens the menu to close the event, such as OnDrawerOpened () and onDrawerClosed ().

Item 4:If you use the Activity Bar action, you can use the Support library provided by the ActionBarDrawerToggle class, the class implements the DrawerLayout.DrawerListener interface,And you can rewrite the related functions as needed. The class implements the menu and bar Action related operations.When the menu is displayed, you should hide the function menu on the ActionBar and modify the title of the ActionBar. ActionBarDrawerToggle class to achieve these functions.

Details of the official website:Http://developer.android.com/design/patterns/navigation-drawer.html.

top
Zero
step on
Zero
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit44176 times
    • Integral:Two thousand one hundred and thirty-five
    • Grade
    • Rank:10733rd name
    • Original175
    • Reproduced:37
    • Translation:1
    • Comments:10
    Latest comments