Combination of DrawerLayout and Fragment
1. xml Code
<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" > <FrameLayout android:id="@+id/fragment_layout" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <RelativeLayout android:id="@+id/menu_layout" android:layout_width="300dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#ff333333"> <ListView android:id="@+id/menu_listView" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout></android.support.v4.widget.DrawerLayout>
2. MainActivity inherits FragmentActivity
Import android. OS. bundle; import android. app. activity; import android. support. v4.app. fragment; import android. support. v4.app. fragmentActivity; import android. support. v4.app. fragmentTransaction; import android. support. v4.widget. drawerLayout; import android. view. menu; import android. view. view; import android. widget. adapterView; import android. widget. arrayAdapter; import android. widget. listView; import android. w Idget. relativeLayout; import android. widget. adapterView. onItemClickListener; public class MainActivity extends FragmentActivity {/** combination of DrawerLayout and Fragment * DrawerLayout: * 1. the general content layer uses framelayout * 2. for slidingLayout, you need to set a layout_grative attribute *. For this document, we recommend that you use android: layout_gravity = "start" */public static final String [] TITLES = {"Henry IV (1 )", "Henry V", "Henry VIII", "Richard II", "Richard III", "Merchant Of Venice "," Othello "," King Lear "}; private DrawerLayout mDrawer_layout; private RelativeLayout mMenu_layout; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mDrawer_layout = (DrawerLayout) findViewById (R. id. drawer_layout); mMenu_layout = (RelativeLayout) findViewById (R. id. menu_layout); ListView menu_listv Iew = (ListView) mMenu_layout.findViewById (R. id. menu_listView); menu_listview.setAdapter (new ArrayAdapter <String> (this, android. r. layout. simple_expandable_list_item_1, TITLES); // listener (new DrawerItemClickListener ();} public class DrawerItemClickListener implements OnItemClickListener {@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {FragmentTransaction ft = getSupportFragmentManager (). beginTransaction (); Fragment fragment = null; // determine which Fragment switch (position) {case 0: fragment = new FirstFragment (); break; case 1: fragment = new NextFragment (); break; default: break;} ft. replace (R. id. fragment_layout, fragment); ft. commit (); mDrawer_layout.closeDrawer (mMenu_layout); // close mMenu_layout }}@ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
3. Fragment class
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FirstFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.firstlayout, null); return layout; }}
public class NextFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.nextlayout, null); return layout; }}
This article is from the "no trace in the sky but I fly over" blog. For more information, contact the author!