布局:fg_left_drawer
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/list_left_listview" android:layout_width="180dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:background="#d6d6d6" android:divider="#FFFFFF" android:dividerHeight="1dp" /> </LinearLayout>
创建LeftDrawFragment
public class LeftDrawFragment extends Fragment{ private ListView listView; private ArrayAdapter<String> myAdapter = null; private String []data = {"1","2","3","4"}; private DrawerLayout drawerLayout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_left_drawer, container, false); // tv_content = (TextView) view.findViewById(R.id.tv_content); // String text = getArguments().getString("text"); // tv_content.setText(text); listView = (ListView)view.findViewById(R.id.list_left_listview); myAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,data); listView.setAdapter(myAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { drawerLayout.closeDrawer(Gravity.LEFT); Toast.makeText(getActivity(),"点击了"+data[i],Toast.LENGTH_SHORT).show(); } }); return view; } //暴露给Activity,用于传入DrawerLayout,因为点击后想关掉DrawerLayout public void setDrawerLayout(DrawerLayout drawer_layout){ this.drawerLayout = drawer_layout; } }
第三步,default_nav_head.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="64dp" android:background="#12b7f5"> <LinearLayout android:layout_width="85dp" android:layout_height="match_parent" android:id="@+id/default_nav_left_layout"> <ImageView android:layout_width="45dp" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:layout_marginLeft="5dp" /> </LinearLayout> <TextView android:layout_width="80dp" android:layout_height="match_parent" android:layout_weight="1" android:text="档案" android:gravity="center" android:textColor="#ffffff" android:textSize="15dp" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" /> <RelativeLayout android:layout_width="85dp" android:layout_height="match_parent" android:id="@+id/default_nav_right_layout" > <ImageView android:layout_width="45dp" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:layout_alignParentRight="true" android:layout_marginRight="5dp" /> </RelativeLayout> </LinearLayout>
fg_content.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/home_content_gridview" android:numColumns="auto_fit" android:columnWidth="90dp" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:layout_margin="5dp"> </GridView> </RelativeLayout>
创建ContentFragment
public class ContentFragment extends Fragment{ private GridView gridView; private MainAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_content, container, false); // tv_content = (TextView) view.findViewById(R.id.tv_content); // String text = getArguments().getString("text"); // tv_content.setText(text); gridView = (GridView) view.findViewById(R.id.home_content_gridview); adapter = new MainAdapter(getActivity()); gridView.setAdapter(adapter); return view; } }
activity_xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/default_nav_head"/> <FrameLayout android:id="@+id/ly_content" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:tag="LEFT" android:layout_gravity="left" android:name="com.example.mac.mainapplication.Activity.LeftDrawFragment" android:id="@+id/left_drawer_fragment"/> </android.support.v4.widget.DrawerLayout>
mainactivity
public class MainActivity extends BaseActivity implements View.OnClickListener { private DrawerLayout drawerLayout; private LeftDrawFragment leftDrawFragment; private FragmentManager fManager; private LinearLayout leftLayout; private RelativeLayout rightLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fManager = getSupportFragmentManager(); leftDrawFragment = (LeftDrawFragment)fManager.findFragmentById(R.id.left_drawer_fragment); drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); leftDrawFragment.setDrawerLayout(drawerLayout); ContentFragment contentFragment = new ContentFragment(); Bundle args = new Bundle(); args.putString("text",""); contentFragment.setArguments(args); FragmentManager fm = getSupportFragmentManager(); fm.beginTransaction().replace(R.id.ly_content,contentFragment).commit(); leftLayout = (LinearLayout)findViewById(R.id.default_nav_left_layout); leftLayout.setOnClickListener(this); rightLayout = (RelativeLayout)findViewById(R.id.default_nav_right_layout); rightLayout.setVisibility(View.INVISIBLE); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.default_nav_left_layout: drawerLayout.openDrawer(Gravity.LEFT); break; default: break; } } }
数据参考:http://blog.csdn.net/coder_pig/article/details/49000185