主界面
package com.wzq.drawerlayoutdemo2;
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.FrameLayout;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private FrameLayout frameLayout;
private ListView lv;
private ArrayList<String> lists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
setListener();
}
private void initView() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
lv = (ListView) findViewById(R.id.lv);
}
private void initData() {
lists = new ArrayList<>();
for (int i = 0; i < 5; i++) {
lists.add("DrawerLayout" + i);
}
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, lists);
lv.setAdapter(stringArrayAdapter);
}
private void setListener() {
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MFragment mFragment = new MFragment();
Bundle bundle = new Bundle();
bundle.putString("name", lists.get(i));
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, mFragment).commit();
//关闭drawer
drawerLayout.closeDrawer(lv);
}
});
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wzq.drawerlayoutdemo2.MainActivity">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
和drawerLayout配合需要配这两个属性
android:layout_gravity="left"从左侧拉出
android:choiceMode="singleChoice"
-->
<ListView
android:id="@+id/lv"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/colorPrimary"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
侧滑页面
package com.wzq.drawerlayoutdemo2;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstancState) {
TextView textView = new TextView(getActivity());
Bundle arguments = getArguments();
String name = arguments.getString("name");
textView.setText(name);
return textView;
}
}