android侧滑菜单ui,Android:UI控件DrawerLayout、Fragment、SlidingLayout、侧滑菜单

DrawerLayout与Fragment的联用

1.xml代码

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/drawer_layout"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/fragment_layout"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/menu_layout"

android:layout_width="300dp"

android:layout_height="match_parent"

android:layout_gravity="left"

android:background="#ff333333">

android:id="@+id/menu_listView"

android:layout_width="match_parent"

android:layout_height="match_parent" >

2.MainActivity继承FragmentActivityimport android.os.Bundle;

import android.app.Activity;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentTransaction;

import android.support.v4.widget.DrawerLayout;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.RelativeLayout;

import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends FragmentActivity

{

/*

* DrawerLayout与Fragment的联用

* DrawerLayout相关:

* 1.一般内容层使用framelayout

* 2.slidingLayout需要设置一个layout_grative属性

* 文档建议使用android:layout_gravity="start"

*/

public static final String[] TITLES =

{ "Henry IV (1)", "Henry V", "Henry VIII", "Richard II", "Richard III", "Merchant of Venice", "Othello", "King Lear" };

private DrawerLayout mDrawer_layout;

private RelativeLayout mMenu_layout;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mDrawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);

mMenu_layout = (RelativeLayout) findViewById(R.id.menu_layout);

ListView menu_listview = (ListView) mMenu_layout.findViewById(R.id.menu_listView);

menu_listview.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, TITLES));

//监听菜单

menu_listview.setOnItemClickListener(new DrawerItemClickListener());

}

public class DrawerItemClickListener implements OnItemClickListener

{

@Override

public void onItemClick(AdapterView> parent, View view, int position, long id)

{

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

Fragment fragment = null;

//根据item点击行号判断启用哪个Fragment

switch (position)

{

case 0:

fragment = new FirstFragment();

break;

case 1:

fragment = new NextFragment();

break;

default:

break;

}

ft.replace(R.id.fragment_layout, fragment);

ft.commit();

mDrawer_layout.closeDrawer(mMenu_layout);//点击后关闭mMenu_layout

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu)

{

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

3.Fragment类import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

public class FirstFragment extends Fragment

{

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

{

View layout = inflater.inflate(R.layout.firstlayout, null);

return layout;

}

}

public class NextFragment extends Fragment

{

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

{

View layout = inflater.inflate(R.layout.nextlayout, null);

return layout;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值