1.主页MainActvty
private DrawerLayout dl;
private ListView lv;
private List<String> list = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dl = (DrawerLayout) findViewById(R.id.dl);
lv = (ListView) findViewById(R.id.lv);
for (int x = 0;x<10;x++){
list.add("item"+x);
}
lv.setAdapter(new MyAdapter());
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("text",list.get(position));
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fl,fragment).commit();
dl.closeDrawer(lv);
}
});
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setText(list.get(position));
tv.setTextSize(20);
return tv;
}
}
}
2.Fragment
Bundle bundle = getArguments();
String text = bundle.getString("text");
TextView tv = new TextView(getActivity());
tv.setText(text);
return tv;
3.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
<ListView
android:id="@+id/lv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#0f0"
android:choiceMode="singleChoice"></ListView>
</android.support.v4.widget.DrawerLayout>