声明:这段程序是从极客学院学习而来,我在学习时加入了一些自己的备注,供大家参考,极客学院的源代码地址:http://download.csdn.net/detail/wo_ha/9493799
activity_main.xml文件中
主内容区的布局代码要放在侧滑菜单布局的前面,这可以帮助DrawerLayout判断谁是侧滑菜单,谁是主内容区
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--主要内容的视图-->
<FrameLayout
android:id="@+id/connect_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!--抽屉视图-->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffcccc"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp">
</ListView>
<!--
android:layout_width="240dp" 抽屉的宽度
android:layout_height="match_parent"
android:layout_gravity="start" 抽屉的滑出方式,start为从左往右滑出,end为从右往左滑出(非常重要的属性)
android:choiceMode="singleChoice" 设置选择模式,单选模式
android:divider="@android:color/transparent" 设置分割线
android:dividerHeight="0dp" 设置分割线的高度-->
</android.support.v4.widget.DrawerLayout>
MainActivity.class文件中
package com.example.yuxue.learndrawerlayout;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private DrawerLayout drawerLayout;
private ListView drawerlist; //定义一个抽屉列表
private ArrayList<String> arrayList; //定义一个数组列表
private ArrayAdapter<String> arrayAdapter; //定义一个数组适配器
private android.support.v4.app.ActionBarDrawerToggle actionBarDrawerToggle;
private String bartitle;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bartitle = (String) getTitle(); //获取当前标题显示是文字
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerlist = (ListView) findViewById(R.id.left_drawer);
//往数组列表中添加数据
arrayList = new ArrayList<String>();
for (int i = 1; i < 6; i++) {
arrayList.add("jikexueyuan :" + i);
}
//新建一个ArrayAdapter并设置数组元素
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
//设置抽屉的适配器
drawerlist.setAdapter(arrayAdapter);
//为抽屉列表添加点击时间的监听
drawerlist.setOnItemClickListener(this);
actionBarDrawerToggle = new android.support.v4.app.ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.Drawer_Open, R.string.Drawer_Close) {
//当抽屉打开时,更改标题的文字
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Log.e("MainActivity", "onDrawerOpened>>>>>>>");
setTitle("请选择");
}
//抽屉关闭恢复原来的文字
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
setTitle(bartitle);
Log.e("MainActivity", "onDrawerClosed>>>>>>>");
}
};
//设置抽屉的监听事件
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}
//为抽屉列表添加点击时间的监听
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//动态插入一个Fragment到FrameLayout中 (即插入到主要内容显示区中)
//新建一个Fragment(自定义的)
Fragment connectFragment = new ConnectFragment();
//新建一个Bundle,将数据传给新的Fragment并显示
Bundle bundle = new Bundle();
bundle.putString("text", arrayList.get(position));
connectFragment.setArguments(bundle);
//获取当前的Fragment
android.app.FragmentManager fragmentManager = getFragmentManager();
//将当前的Fragment替换为新建的Fragment并提交这个事务
fragmentManager.beginTransaction().replace(R.id.connect_frame, connectFragment).commit();
//关闭抽屉(显示新的Fragment)
drawerLayout.closeDrawer(drawerlist);
}
}
fragment_connect.xml文件中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp" />
</LinearLayout>
ConnectFragment.class文件中
package com.example.yuxue.learndrawerlayout;
//这个类用于新建的Fragment的显示界面,绑定的界面为fragment_connect.xml
public class ConnectFragment extends android.app.Fragment {
private TextView textView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_connect,container,false);
textView=(TextView)view.findViewById(R.id.textview);
String text=getArguments().getString("text");
textView.setText(text);
return view;
}
}
如果我的教程完美的解决了你的问题,给个赞或者评论一下吧,你们的回复是我写作的动力!