content_frame.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/content_frame" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" />
左侧 menu_frame.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/menu_frame" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" />
右侧 menu_frame_right.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/menu_frame_right" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" />
1 package com.cn.slidingmenu.fragment; 2 3 import com.actionbarsherlock.view.MenuItem; 4 import com.cn.slidingmenu.fragment.R; 5 import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; 6 import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.CanvasTransformer; 7 import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; 8 9 import android.graphics.Canvas; 10 import android.os.Bundle; 11 import android.support.v4.app.Fragment; 12 import android.view.animation.Interpolator; 13 14 /** 15 * slidingmenu + fragment 16 * 17 * @author dr 18 */ 19 public class MainActivity extends SlidingFragmentActivity { 20 21 private SlidingMenu sm; 22 private Fragment contentFragment; 23 private Fragment leftFragment; 24 25 @Override 26 public void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.content_frame); 29 30 // 设置SlidingMenu的layout 31 setBehindContentView(R.layout.menu_frame); 32 33 if (savedInstanceState == null) { 34 contentFragment = new Fragment1(); 35 } else { 36 // 取出之前保存的contentFragment 37 contentFragment = this.getSupportFragmentManager().getFragment( 38 savedInstanceState, "contentFragment"); 39 } 40 // 设置当前的fragment 41 // getSupportFragmentManager JAR包当中的 42 this.getSupportFragmentManager().beginTransaction() 43 .replace(R.id.content_frame, contentFragment).commit(); 44 45 /** 设置SlidingMenu属性和layout */ 46 // 得到SlidingMenu的对象 47 sm = getSlidingMenu(); 48 // 设置slidingmenu滑动的方式 49 sm.setMode(SlidingMenu.LEFT_RIGHT); 50 // 设置slidingmenu边界的阴影图片 51 sm.setShadowDrawable(R.drawable.shadow); 52 // 设置阴影的宽度 53 sm.setShadowWidthRes(R.dimen.shadow_width); 54 // 设置slidingmenu宽度 55 sm.setBehindOffsetRes(R.dimen.slidingmenu_offset); 56 // 设置slidingmenu宽度 57 // sm.setBehindWidth(400); 58 // 设置滑出slidingmenu范围 59 sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); 60 sm.setMenu(R.layout.menu_frame); 61 // 设置slidingmenu动画 62 sm.setBehindCanvasTransformer(null); 63 64 // 设置menu的fragment 65 leftFragment = new LeftFragment(); 66 this.getSupportFragmentManager().beginTransaction() 67 .replace(R.id.menu_frame, leftFragment).commit(); 68 69 // 设置右侧的slidingmenu 70 sm.setSecondaryMenu(R.layout.menu_frame_right); 71 sm.setSecondaryShadowDrawable(R.drawable.shadowright); 72 73 // 设置右侧滑出的slidingmenu 74 RightFragment rightFragment = new RightFragment(); 75 this.getSupportFragmentManager().beginTransaction() 76 .replace(R.id.menu_frame_right, rightFragment).commit(); 77 78 // 设置在slidingmenu页显示ActionBar 79 setSlidingActionBarEnabled(false); 80 // 是ActionBar的图标可以被点击 81 getSupportActionBar().setHomeButtonEnabled(true); 82 // 启用向左的图标 83 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 84 85 } 86 87 @Override 88 public boolean onMenuItemSelected(int featureId, MenuItem item) { 89 switch (item.getItemId()) { 90 case android.R.id.home: 91 // 动态打开或关闭slidingmenu 92 sm.toggle(); 93 break; 94 default: 95 break; 96 } 97 return true; 98 } 99 100 @Override 101 protected void onSaveInstanceState(Bundle outState) { 102 super.onSaveInstanceState(outState); 103 // 1.bundle 104 // 2.存放的ID 105 // 3.当前要保存的fragment的实例 106 this.getSupportFragmentManager().putFragment(outState, 107 "contentFragment", contentFragment); 108 } 109 110 /** 111 * 切换fragment 112 * 113 * @param f 114 */ 115 public void switchFragment(Fragment f) { 116 contentFragment = f; 117 this.getSupportFragmentManager().beginTransaction() 118 .replace(R.id.content_frame, contentFragment).commit(); 119 sm.toggle(); 120 } 121 122 /** 123 * 缩放动画 124 */ 125 private CanvasTransformer canvasTransformer = new CanvasTransformer() { 126 127 @Override 128 public void transformCanvas(Canvas canvas, float percentOpen) { 129 float scale = (float) (percentOpen * 0.25 + 0.75); 130 canvas.scale(scale, scale, canvas.getWidth() / 2, 131 canvas.getHeight() / 2); 132 } 133 134 }; 135 private static Interpolator interp = new Interpolator() { 136 @Override 137 public float getInterpolation(float t) { 138 t -= 1.0f; 139 return t * t * t + 1.0f; 140 } 141 }; 142 143 /** 144 * 从下网上动画 145 */ 146 private CanvasTransformer canvasTransformer2 = new CanvasTransformer() { 147 148 @Override 149 public void transformCanvas(Canvas canvas, float percentOpen) { 150 canvas.translate( 151 0, 152 canvas.getHeight() 153 * (1 - interp.getInterpolation(percentOpen))); 154 } 155 156 }; 157 158 /** 159 * 拉伸动画 160 */ 161 private CanvasTransformer canvasTransformer3 = new CanvasTransformer() { 162 @Override 163 public void transformCanvas(Canvas canvas, float percentOpen) { 164 canvas.scale(percentOpen, 1, 0, 0); 165 166 } 167 }; 168 169 }
1 package com.cn.slidingmenu.fragment; 2 3 import com.cn.slidingmenu.fragment.R; 4 import android.os.Bundle; 5 import android.support.v4.app.Fragment; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.TextView; 10 11 public class Fragment1 extends Fragment { 12 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 } 17 @Override 18 public View onCreateView(LayoutInflater inflater, ViewGroup container, 19 Bundle savedInstanceState) { 20 //设置fragment的layout 21 View view = inflater.inflate(R.layout.text, null); 22 TextView tv = (TextView) view.findViewById(R.id.test); 23 tv.setText(Fragment1.class.getSimpleName()); 24 return view; 25 } 26 @Override 27 public void onActivityCreated(Bundle savedInstanceState) { 28 super.onActivityCreated(savedInstanceState); 29 } 30 31 }
1 package com.cn.slidingmenu.fragment; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import com.cn.slidingmenu.fragment.R; 6 import android.os.Bundle; 7 import android.support.v4.app.Fragment; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.AdapterView; 12 import android.widget.AdapterView.OnItemClickListener; 13 import android.widget.ArrayAdapter; 14 import android.widget.ListView; 15 16 public class LeftFragment extends Fragment implements OnItemClickListener { 17 18 private View rootView; 19 20 @Override 21 public void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 } 24 25 @Override 26 public View onCreateView(LayoutInflater inflater, ViewGroup container, 27 Bundle savedInstanceState) { 28 // 设置fragment的layout 29 rootView = inflater.inflate(R.layout.activity_main, null); 30 return rootView; 31 } 32 33 @Override 34 public void onActivityCreated(Bundle savedInstanceState) { 35 super.onActivityCreated(savedInstanceState); 36 ListView lv = (ListView) rootView.findViewById(R.id.listview); 37 38 List<String> list = initData(); 39 40 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 41 android.R.layout.simple_list_item_1, android.R.id.text1, list); 42 43 lv.setAdapter(adapter); 44 lv.setOnItemClickListener(this); 45 } 46 47 /** 48 * 初始化数据 49 * @return 50 */ 51 public List<String> initData() { 52 List<String> list = new ArrayList<String>(); 53 list.add(Fragment1.class.getSimpleName()); 54 list.add(Fragment2.class.getSimpleName()); 55 list.add(Fragment3.class.getSimpleName()); 56 list.add(Fragment4.class.getSimpleName()); 57 list.add(Fragment5.class.getSimpleName()); 58 return list; 59 } 60 61 @Override 62 public void onItemClick(AdapterView<?> parent, View view, int position, 63 long id) { 64 Fragment f = null; 65 switch (position) { 66 case 0: 67 f = new Fragment1(); 68 break; 69 case 1: 70 f = new Fragment2(); 71 break; 72 case 2: 73 f = new Fragment3(); 74 break; 75 case 3: 76 f = new Fragment4(); 77 break; 78 case 4: 79 f = new Fragment5(); 80 break; 81 default: 82 f = new Fragment1(); 83 break; 84 } 85 switchFragment(f); 86 } 87 88 /** 89 * listview item 点击时切换fragment 90 * @param f 91 */ 92 private void switchFragment(Fragment f) { 93 if (getActivity() instanceof MainActivity) { 94 MainActivity activity = (MainActivity) getActivity(); 95 activity.switchFragment(f); 96 } 97 } 98 99 }
1 package com.cn.slidingmenu.fragment; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import android.os.Bundle; 6 import android.support.v4.app.Fragment; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.ArrayAdapter; 11 import android.widget.ListView; 12 13 public class RightFragment extends Fragment { 14 15 private View rootView; 16 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 } 21 22 @Override 23 public View onCreateView(LayoutInflater inflater, ViewGroup container, 24 Bundle savedInstanceState) { 25 rootView = inflater.inflate(R.layout.activity_main, null); 26 return rootView; 27 } 28 @Override 29 public void onActivityCreated(Bundle savedInstanceState) { 30 super.onActivityCreated(savedInstanceState); 31 ListView lv = (ListView) rootView.findViewById(R.id.listview); 32 List<String> list = initData(); 33 ArrayAdapter<String> adapter = new ArrayAdapter<String>( 34 getActivity(), 35 android.R.layout.simple_list_item_1, 36 android.R.id.text1, 37 list); 38 39 lv.setAdapter(adapter); 40 } 41 42 /** 43 * 初始化数据 44 * @return 45 */ 46 public List<String> initData() { 47 List<String> list = new ArrayList<String>(); 48 list.add(Fragment1.class.getSimpleName()); 49 list.add(Fragment2.class.getSimpleName()); 50 list.add(Fragment3.class.getSimpleName()); 51 list.add(Fragment4.class.getSimpleName()); 52 list.add(Fragment5.class.getSimpleName()); 53 return list; 54 } 55 56 }
DEMO下载地址:http://pan.baidu.com/s/1o6v7iX0
文件中三个包, SlidingMenuFragment21引用library,library引用actionbarsherlock。