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 ListView lv;
ArrayList<String> list = new ArrayList<String>();
private FrameLayout fl_content;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
fl_content = (FrameLayout) findViewById(R.id.fl);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
initData();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MFragment mFragment = MFragment.getInstace(list.get(i));
getSupportFragmentManager().beginTransaction().replace(R.id.fl, mFragment).commit();
//关闭drawerLayout
drawerLayout.closeDrawer(lv);
}
});
}
private void initData() {
for (int i = 0; i < 10; i++) {
list.add("DrawerLayout" + i);
}
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(mAdapter);
}
}
fragment:
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;
/**
* author:Created by WangZhiQiang on 2017-11-09.
*/
public class MFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
Bundle bundle = getArguments();
String name = (String) bundle.get("name");
textView.setText(name);
return textView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
//静态方法,把自己返回;并且可以把传过来的参数封装到自己里面;
public static MFragment getInstace(String s) {
MFragment mFragment = new MFragment();
Bundle bundle = new Bundle();
bundle.putString("name",s);
mFragment.setArguments(bundle);
return mFragment;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView
android:id="@+id/lv"
android:background="@color/colorAccent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:layout_width="150dp"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>