1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#0ff"
android:text="Hello World!" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/dl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主布局必须在 左右两侧子布局的上面 设置-->
<LinearLayout
android:id="@+id/ll_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是主布局内容"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_left"
android:orientation="vertical"
android:layout_gravity="left"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="我是左侧布局"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_right"
android:orientation="vertical"
android:layout_gravity="right"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="我是右侧布局"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
2.主类
public class MainActivity extends AppCompatActivity {
DrawerLayout dl ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dl = (DrawerLayout) findViewById(R.id.dl);
findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置左右侧拉时 使用的是left 和 right 被替代
//start end
//打开侧拉视图
dl.openDrawer(Gravity.LEFT);
// dl.closeDrawer(Gravity.LEFT);
}
});
dl.addDrawerListener(new DrawerLayout.DrawerListener() {
/**
* 当DrawerLayout被拖动的时候 会被调用
* @param drawerView 指当前的DrawerLayout对象
* @param slideOffset 拖动的距离值 0-1
*/
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
Log.i("==========", "onDrawerSlide: " + slideOffset);
}
/**
* 当DrawerLayout控件 完全被打开时 调用
* @param drawerView
*/
@Override
public void onDrawerOpened(View drawerView) {
Log.i("==========", "onDrawerOpened: " + "Opened");
}
@Override
public void onDrawerClosed(View drawerView) {
Log.i("==========", "onDrawerClosed: " + "Closed");
}
/**
* 1、固定状态
* 2、空闲状态
* 3、被拖拽状态
* @param newState
*/
@Override
public void onDrawerStateChanged(int newState) {
switch (newState){
case DrawerLayout.STATE_DRAGGING:
Log.e("==========", "onDrawerStateChanged: STATE_DRAGGING" + newState);
break;
case DrawerLayout.STATE_IDLE:
Log.e("==========", "onDrawerStateChanged: STATE_IDLE" + newState);
break;
case DrawerLayout.STATE_SETTLING:
Log.e("==========", "onDrawerStateChanged: STATE_SETTLING" + newState);
break;
}
}
});
}
}