<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnOpen"
android:text="Hello!!!"
/>
<!--用来放置碎片的容器-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frconten"
android:layout_below="@id/btnOpen"
>
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/menu"
android:layout_width="130dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f00"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"
android:layout_below="@id/img"
>
</ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
package com.example.drawerlayout;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<String>data = new ArrayList<>();
private ListView lv;
private DrawerLayout drawer;
private RelativeLayout menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btnOpen);
drawer = findViewById(R.id.drawer);
menu = findViewById(R.id.menu);
//添加监听
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawer.openDrawer(menu);//打开菜单
}
});
lv = findViewById(R.id.lv);
for (int i= 0;i<20;i++){
data.add("item"+i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,data);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//关闭菜单
drawer.closeDrawer(menu);
//动态添加fragment
getSupportFragmentManager().beginTransaction().replace(R.id.frconten,new MyFragment()).commit();
}
});
//添加监听事件
drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
Toast.makeText(MainActivity.this,"打开了!",Toast.LENGTH_SHORT).show();}
@Override
public void onDrawerClosed(View drawerView) {
Toast.makeText(MainActivity.this,"关闭了!!!",Toast.LENGTH_SHORT).show();;
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
}