使用DrawerLayout的步驟:
①在佈局文件中添加<android.support.v4.widget.DrawerLayout> 根元素
②在這個根元素中首先添加一個 內容視圖組件,比如:我們添加一個<FrameLayout>容器
③再在這個根元素中添加側滑菜單視圖組件,一般來說就是一個ListView組件
④爲ListView設定Adapter,和點擊事件監聽器
⑤爲DrawerLayout添加 開、關 狀態監聽器
View層:
activity_main.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">
<!--main layout-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!--navigation list view-->
<!--
android:layout_gravity="start"表示:從左向右滑出菜單
android:choiceMode="singleChoice"表示:菜單爲單選模式
android:divider="@android:color/transparent"
android:dividerHeight="0dp"表示:菜單項分隔線顏色和高度
-->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:background="#3E86A0"
android:divider="@android:color/transparent"
android:dividerHeight="0dp">
</ListView>
</android.support.v4.widget.DrawerLayout>
fragment_content.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContentFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Control層:
MainActivity.xml
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private DrawerLayout drawerLayout;
private ListView listView;
private ArrayList<String> list;
private ArrayAdapter<String> adapter;
private String title;
private ActionBarDrawerToggle toggle;
/*
drawerLayout.setDrawerListener(DrawerLayout.DrawerListener),ActionBarDrawerToggle是
DrawerLayout.DrawerListener的具體實現類(功能:1.改變android.R.id.home圖標(構造方法)
2.Drawer拉出、隱藏,帶有android.R.id.home動畫效果(syncState())
3.監聽Drawer拉出、隱藏事件)。重寫兩個方法onDrawerOpened()和onDrawerClosed()來幾件聽抽屜的拉出和隱藏
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title= (String) getTitle();
drawerLayout=findViewById(R.id.drawer_layout);
listView=findViewById(R.id.left_drawer);
list=new ArrayList<>();//數據集合
for(int i=1;i<10;i++)
list.add("計算機科學與技術0"+i);
adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//動態插入一個Fragment到FrameLayout中
Fragment fragment=new ContentFragment(); //定義一個fragment
Bundle bundle=new Bundle();// 使用bundle傳遞數據
bundle.putString("data",list.get(position));
fragment.setArguments(bundle); //fragment攜帶上數據,通過FragmentManager傳遞到fragment中
FragmentManager fragmentManager=getSupportFragmentManager();//獲取fragment管理對象
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();//開啓事務代替content_frame
drawerLayout.closeDrawer(listView);//主佈局關掉listView
}
}
ContentFragment.java
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=view.findViewById(R.id.text);
textView.setText(getArguments().getString("data"));//getArguments():獲取傳進來的參數
return view;
}
}
運行效果: