Drawerlayout

mydrawerlayout.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.v4.widget.DrawerLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:id="@+id/id_drawer_layout"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent" >
 7 
 8     <!-- The main content view -->
 9 
10     <FrameLayout
11         android:id="@+id/id_content_frame"
12         android:layout_width="match_parent"
13         android:layout_height="match_parent" >
14     </FrameLayout>
15 
16     <!-- the navigation view  -->
17 
18     <ListView
19         android:id="@+id/id_left_drawer"
20         android:layout_width="240dp"
21         android:layout_height="match_parent"
22         android:layout_gravity="start"
23         android:background="#ffffcc"
24         android:choiceMode="singleChoice"
25         android:dividerHeight="0dp" >
26     </ListView>
27 
28 </android.support.v4.widget.DrawerLayout>

ContentFragment.java

 1 package com.example.suneyaenews;
 2 
 3 import android.app.Fragment;
 4 import android.os.Bundle;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.TextView;
 9 
10 public class ContentFragment extends Fragment {
11     private TextView tv;
12 
13     @Override
14     public View onCreateView(LayoutInflater inflater, ViewGroup container,
15             Bundle savedInstanceState) {
16         View view = inflater.inflate(R.layout.fragment_content, container,
17                 false);
18         tv = (TextView) view.findViewById(R.id.id_tv_fraglayout);
19         String text = getArguments().getString("text");
20         tv.setText(text);
21         // return tv;//会报错,报 The specified child already has a parent. You must
22         // call removeView() on the child's parent first.
23         return view;// 这样就不会报错
24     }
25 }
 1 package com.example.suneyaenews;
 2 
 3 import java.util.ArrayList;
 4 
 5 import android.app.Activity;
 6 import android.app.Fragment;
 7 import android.app.FragmentManager;
 8 import android.os.Bundle;
 9 import android.support.v4.widget.DrawerLayout;
10 import android.view.View;
11 import android.widget.AdapterView;
12 import android.widget.ArrayAdapter;
13 import android.widget.FrameLayout;
14 import android.widget.ListView;
15 import android.widget.AdapterView.OnItemClickListener;
16 
17 public class DrawerActivity extends Activity{
18     private ListView lv_drawer;
19     private FrameLayout frame_drawer;
20     private DrawerLayout mDrawerlayout;
21     private ArrayList<String> mMenulist;
22     private ArrayAdapter<String> adapter ;
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         // TODO Auto-generated method stub
26         super.onCreate(savedInstanceState);
27         // android.support.v4.widget.DrawerLayout 布局
28         setContentView(R.layout.mydrawerlayout);
29         mDrawerlayout = (DrawerLayout) findViewById(R.id.id_drawer_layout);
30         lv_drawer = (ListView) findViewById(R.id.id_left_drawer);
31         mMenulist = new ArrayList<String>();
32         for (int i = 0; i < 6; i++) {
33             mMenulist.add("listview" + i);
34         }
35         adapter = new ArrayAdapter<String>(this,
36                 android.R.layout.simple_list_item_1, mMenulist);
37         lv_drawer.setAdapter(adapter);
38         lv_drawer.setOnItemClickListener(new OnItemClickListener() {
39 
40             @Override
41             public void onItemClick(AdapterView<?> parent, View view,
42                     int position, long id) {
43                 // TODO Auto-generated method stub
44                 //动态插入一个fragment到framelayout
45                 System.out.println("........111111111111");
46                 Fragment contentFrament = new ContentFragment();
47                 Bundle bundle = new Bundle();
48                 bundle.putString("text", mMenulist.get(position));
49                 contentFrament.setArguments(bundle);
50                 System.out.println("........2222222222222");
51                 FragmentManager fm = getFragmentManager();
52                 fm.beginTransaction().replace(R.id.id_content_frame,contentFrament).commit();
53                 mDrawerlayout.closeDrawer(lv_drawer);//关闭listview
54                 System.out.println("........33333333333333");
55             }
56         });
57     }
58 }
原文地址:https://www.cnblogs.com/Sunnor/p/5479827.html