码迷,mamicode.com
首页 > 其他好文 > 详细

DrawerLayout使用

时间:2015-09-18 23:12:27      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

布局:

技术分享
 1 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:id="@+id/drawer_layout"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <!-- The main content view -->
 7 
 8     <FrameLayout
 9         android:id="@+id/content_frame"
10         android:layout_width="match_parent"
11         android:layout_height="match_parent" >
12     </FrameLayout>
13 
14     <!-- The navigation view -->
15 
16     <ListView
17         android:id="@+id/left_drawer"
18         android:layout_width="240dp"
19         android:layout_height="match_parent"
20         android:layout_gravity="start"
21         android:background="#ffffcc"
22         android:choiceMode="singleChoice"
23         android:divider="@android:color/transparent"
24         android:dividerHeight="0dp" >
25     </ListView>
26 
27 </android.support.v4.widget.DrawerLayout>
Layout

主界面:

技术分享
  1 package com.jikexueyuan.drawerlayoutusing;
  2 
  3 import java.util.ArrayList;
  4 
  5 import android.net.Uri;
  6 import android.os.Bundle;
  7 import android.R.anim;
  8 import android.app.Activity;
  9 import android.app.Fragment;
 10 import android.app.FragmentManager;
 11 import android.content.Intent;
 12 import android.content.res.Configuration;
 13 import android.support.v4.app.ActionBarDrawerToggle;
 14 import android.support.v4.widget.DrawerLayout;
 15 import android.view.Menu;
 16 import android.view.MenuItem;
 17 import android.view.View;
 18 import android.widget.AdapterView;
 19 import android.widget.AdapterView.OnItemClickListener;
 20 import android.widget.ArrayAdapter;
 21 import android.widget.ListView;
 22 
 23 public class MainActivity extends Activity implements OnItemClickListener {
 24 
 25     private DrawerLayout mDrawerLayout;
 26     private ListView mDrawerList;
 27     private ArrayList<String> menuLists;
 28     private ArrayAdapter<String> adapter;
 29     private ActionBarDrawerToggle mDrawerToggle;
 30     private String mTitle;
 31 
 32     @Override
 33     protected void onCreate(Bundle savedInstanceState) {
 34         super.onCreate(savedInstanceState);
 35         setContentView(R.layout.activity_main);
 36 
 37         mTitle = (String) getTitle();
 38 
 39         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
 40         mDrawerList = (ListView) findViewById(R.id.left_drawer);
 41         menuLists = new ArrayList<String>();
 42         for (int i = 0; i < 5; i++)
 43             menuLists.add("极客学院0" + i);
 44         adapter = new ArrayAdapter<String>(this,
 45                 android.R.layout.simple_list_item_1, menuLists);
 46         mDrawerList.setAdapter(adapter);
 47         mDrawerList.setOnItemClickListener(this);
 48 
 49         mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
 50                 R.drawable.ic_drawer, R.string.drawer_open,
 51                 R.string.drawer_close) {
 52             @Override
 53             public void onDrawerOpened(View drawerView) {
 54                 super.onDrawerOpened(drawerView);
 55                 getActionBar().setTitle("请选择");
 56                 invalidateOptionsMenu(); // Call onPrepareOptionsMenu()
 57             }
 58 
 59             @Override
 60             public void onDrawerClosed(View drawerView) {
 61                 super.onDrawerClosed(drawerView);
 62                 getActionBar().setTitle(mTitle);
 63                 invalidateOptionsMenu();
 64             }
 65         };
 66         mDrawerLayout.setDrawerListener(mDrawerToggle);
 67         
 68         //开启ActionBar上APP ICON的功能
 69         getActionBar().setDisplayHomeAsUpEnabled(true);
 70         getActionBar().setHomeButtonEnabled(true);
 71 
 72     }
 73 
 74     @Override
 75     public boolean onPrepareOptionsMenu(Menu menu) {
 76         boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
 77         menu.findItem(R.id.action_websearch).setVisible(!isDrawerOpen);
 78         return super.onPrepareOptionsMenu(menu);
 79     }
 80 
 81     @Override
 82     public boolean onCreateOptionsMenu(Menu menu) {
 83         // Inflate the menu; this adds items to the action bar if it is present.
 84         getMenuInflater().inflate(R.menu.main, menu);
 85         return true;
 86     }
 87 
 88     @Override
 89     public boolean onOptionsItemSelected(MenuItem item) {
 90         //将ActionBar上的图标与Drawer结合起来
 91         if (mDrawerToggle.onOptionsItemSelected(item)){
 92             return true;
 93         }
 94         switch (item.getItemId()) {
 95         case R.id.action_websearch:
 96             Intent intent = new Intent();
 97             intent.setAction("android.intent.action.VIEW");
 98             Uri uri = Uri.parse("http://www.jikexueyuan.com");
 99             intent.setData(uri);
100             startActivity(intent);
101             break;
102         }
103         return super.onOptionsItemSelected(item);
104     }
105     
106     @Override
107     protected void onPostCreate(Bundle savedInstanceState) {
108         super.onPostCreate(savedInstanceState);
109         //需要将ActionDrawerToggle与DrawerLayout的状态同步
110         //将ActionBarDrawerToggle中的drawer图标,设置为ActionBar中的Home-Button的Icon
111         mDrawerToggle.syncState();
112     }
113     
114     @Override
115     public void onConfigurationChanged(Configuration newConfig) {
116         super.onConfigurationChanged(newConfig);
117         mDrawerToggle.onConfigurationChanged(newConfig);
118     }    
119 
120     @Override
121     public void onItemClick(AdapterView<?> arg0, View arg1, int position,
122             long arg3) {
123         // 动态插入一个Fragment到FrameLayout当中
124         Fragment contentFragment = new ContentFragment();
125         Bundle args = new Bundle();
126         args.putString("text", menuLists.get(position));
127         contentFragment.setArguments(args);
128 
129         FragmentManager fm = getFragmentManager();
130         fm.beginTransaction().replace(R.id.content_frame, contentFragment)
131                 .commit();
132 
133         mDrawerLayout.closeDrawer(mDrawerList);
134     }
135 
136 }
MainActivity

跳转界面:

技术分享
package com.jikexueyuan.drawerlayoutusing;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ContentFragment extends Fragment {
    
    private TextView textView;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content, container, false);
        textView = (TextView) view.findViewById(R.id.textView);
        
        String text = getArguments().getString("text");
        textView.setText(text);
        
        return view;
    }
    
}
ContentFragment

 

Thanks

DrawerLayout使用

标签:

原文地址:http://www.cnblogs.com/myeve/p/4820542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!