现在的位置: 首页 > 编程技术 > 安卓开发 > 正文

android实现在app所有页面实现Navigation Drawer侧滑菜单

2015年11月11日 安卓开发 ⁄ 共 5312字 ⁄ 字号 android实现在app所有页面实现Navigation Drawer侧滑菜单已关闭评论 ⁄ 阅读 1056 次

主要思路是写一个baseActivity,带有DrawerLayout,然后所有的activity extends baseActivity,主要代码如下:

public class BaseActivity extends Activity {
 public DrawerLayout mDrawerLayout;
 public ListView mDrawerList;
 public ImageButton navbtn;
 public TextView navtitle;
 public LinearLayout toolbar;
 public List<Map<String, Object>> mData;
 public FunctionAdapter adapter;
 private static final String TAG = "BaseActivity";

 /**
 * ***************************** 【Activity LifeCycle For Debug】 ******************************************
 */

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 LogUtil.d(TAG, this.getClass().getSimpleName() + " onCreate() invoked!!");
 super.onCreate(savedInstanceState);
 configActivity();
 setContentView(R.layout.activity_base);
 initUI();
 }

 

 @Override
 protected void onNewIntent(Intent intent) {
 super.onNewIntent(intent);
 setIntent(intent);// 必须要调用这句
 }

 /**
 * ***************************** 【Activity LifeCycle For Debug】 ******************************************
 */

 private class DrawerItemClickListener implements ListView.OnItemClickListener {
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 selectItem(position);
 }
 }

 private void initUI() {
 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
 mDrawerList = (ListView) findViewById(R.id.left_drawer);
 navbtn = (ImageButton) findViewById(R.id.navbtn);
 navtitle = (TextView) findViewById(R.id.navtitle);
 toolbar = (LinearLayout) findViewById(R.id.toolbar);
 mData = getData();
 adapter = new FunctionAdapter(getApplicationContext(), mData);
 mDrawerList.setAdapter(adapter);
 mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

 navbtn.setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 onBackPressed();
 }

 });
 }

 private void selectItem(int position) {
 mDrawerLayout.closeDrawer(mDrawerList);
 switch (position) {
 case 0:
 
 break;
 case 1:
 
 break;
 case 2:
 
 break;
 case 3:
 
 break;
 case 4:
 
 break;
 case 5:
 
 break;
 case 6:
 
 break;
 case 7:
 
 break;
 case 8:
 
 break;
 default:
 break;
 }
 }

 public void baseSetContentView(int layoutResId) {
 LinearLayout llContent = (LinearLayout) findViewById(R.id.llContent);
 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View v = inflater.inflate(layoutResId, null);
 llContent.addView(v, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
 }

 private void configActivity() {
 if (!Config.isShowTitle) {
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 }
 if (Config.isFullScreen) {
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
 }
 }

 private List<Map<String, Object>> getData() {
 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 Map<String, Object> map;
 int[] img = {R.drawable.icon_jieqianhua, R.drawable.icon_hongbao, R.drawable.icon_card, R.drawable.icon_bafore, R.drawable.icon_update, R.drawable.icon_share, R.drawable.icon_faq, R.drawable.icon_feedback, R.drawable.icon_settings};
 int[] imgExpired = {R.drawable.icon_jieqianhua, R.drawable.icon_hongbao_expiry, R.drawable.icon_card_expiry, R.drawable.icon_bafore_expiry, R.drawable.icon_update, R.drawable.icon_share, R.drawable.icon_faq, R.drawable.icon_feedback,
 R.drawable.icon_settings};
 String[] titles = getResources().getStringArray(R.array.function);
 for (int i = 0; i < img.length; i++) {
 map = new HashMap<String, Object>();
 map.put("img", img[i]);
 map.put("title", titles[i]);
 map.put("imgExpired", imgExpired[i]);
 map.put("info", 0);
 try {
 if (UserApi.getVersion(getApplicationContext()) > getPackageManager().getPackageInfo(getPackageName(), 0).versionCode && (i == 4)) {
 map.put("info", 1);
 LogUtil.i(TAG, "getPackageManager().getPackageInfo(getPackageName(), 0).versionCode=" + getPackageManager().getPackageInfo(getPackageName(), 0).versionCode);
 }
 } catch (NameNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 list.add(map);
 }
 return list;
 } 

 @Override
 public void onBackPressed() {
 if (mDrawerLayout.isDrawerOpen(mDrawerList))
 mDrawerLayout.closeDrawer(mDrawerList);
 else
 super.onBackPressed();
 }
}

其中主要调用这个方法来设置界面 baseSetContentView(int layoutResId)。对应的xml文件:

<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 -->

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <LinearLayout
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="44dp"
 android:background="#009aff"
 android:orientation="horizontal" >

 <ImageButton
 android:id="@+id/navbtn"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:background="@drawable/back_btn" />

 <TextView
 android:id="@+id/navtitle"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 android:paddingRight="39dp"
 android:textColor="#ffffff"
 android:textSize="17sp" />
 </LinearLayout>

 <LinearLayout
 android:id="@+id/llContent"
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:orientation="vertical" >
 </LinearLayout>
 </LinearLayout>
 <!-- The navigation drawer -->

 <ListView
 android:id="@+id/left_drawer"
 android:layout_width="307dp"
 android:layout_height="match_parent"
 android:layout_gravity="start"
 android:background="#009aff"
 android:choiceMode="singleChoice"
 android:divider="@android:color/transparent"
 android:dividerHeight="0dp"
 android:listSelector="#22a7ff" />

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


参考的是谷歌官方drawerlayout demo:NavigationDrawer

抱歉!评论已关闭.