content_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
左侧 menu_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
右侧 menu_frame_right.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame_right"
android:layout_width="match_parent"
android:layout_height="match_parent" />
package com.cn.slidingmenu.fragment; import com.actionbarsherlock.view.MenuItem;
import com.cn.slidingmenu.fragment.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.CanvasTransformer;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.animation.Interpolator; /**
* slidingmenu + fragment
*
* @author dr
*/
public class MainActivity extends SlidingFragmentActivity { private SlidingMenu sm;
private Fragment contentFragment;
private Fragment leftFragment; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_frame); // 设置SlidingMenu的layout
setBehindContentView(R.layout.menu_frame); if (savedInstanceState == null) {
contentFragment = new Fragment1();
} else {
// 取出之前保存的contentFragment
contentFragment = this.getSupportFragmentManager().getFragment(
savedInstanceState, "contentFragment");
}
// 设置当前的fragment
// getSupportFragmentManager JAR包当中的
this.getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, contentFragment).commit(); /** 设置SlidingMenu属性和layout */
// 得到SlidingMenu的对象
sm = getSlidingMenu();
// 设置slidingmenu滑动的方式
sm.setMode(SlidingMenu.LEFT_RIGHT);
// 设置slidingmenu边界的阴影图片
sm.setShadowDrawable(R.drawable.shadow);
// 设置阴影的宽度
sm.setShadowWidthRes(R.dimen.shadow_width);
// 设置slidingmenu宽度
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// 设置slidingmenu宽度
// sm.setBehindWidth(400);
// 设置滑出slidingmenu范围
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setMenu(R.layout.menu_frame);
// 设置slidingmenu动画
sm.setBehindCanvasTransformer(null); // 设置menu的fragment
leftFragment = new LeftFragment();
this.getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, leftFragment).commit(); // 设置右侧的slidingmenu
sm.setSecondaryMenu(R.layout.menu_frame_right);
sm.setSecondaryShadowDrawable(R.drawable.shadowright); // 设置右侧滑出的slidingmenu
RightFragment rightFragment = new RightFragment();
this.getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame_right, rightFragment).commit(); // 设置在slidingmenu页显示ActionBar
setSlidingActionBarEnabled(false);
// 是ActionBar的图标可以被点击
getSupportActionBar().setHomeButtonEnabled(true);
// 启用向左的图标
getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// 动态打开或关闭slidingmenu
sm.toggle();
break;
default:
break;
}
return true;
} @Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// 1.bundle
// 2.存放的ID
// 3.当前要保存的fragment的实例
this.getSupportFragmentManager().putFragment(outState,
"contentFragment", contentFragment);
} /**
* 切换fragment
*
* @param f
*/
public void switchFragment(Fragment f) {
contentFragment = f;
this.getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, contentFragment).commit();
sm.toggle();
} /**
* 缩放动画
*/
private CanvasTransformer canvasTransformer = new CanvasTransformer() { @Override
public void transformCanvas(Canvas canvas, float percentOpen) {
float scale = (float) (percentOpen * 0.25 + 0.75);
canvas.scale(scale, scale, canvas.getWidth() / 2,
canvas.getHeight() / 2);
} };
private static Interpolator interp = new Interpolator() {
@Override
public float getInterpolation(float t) {
t -= 1.0f;
return t * t * t + 1.0f;
}
}; /**
* 从下网上动画
*/
private CanvasTransformer canvasTransformer2 = new CanvasTransformer() { @Override
public void transformCanvas(Canvas canvas, float percentOpen) {
canvas.translate(
0,
canvas.getHeight()
* (1 - interp.getInterpolation(percentOpen)));
} }; /**
* 拉伸动画
*/
private CanvasTransformer canvasTransformer3 = new CanvasTransformer() {
@Override
public void transformCanvas(Canvas canvas, float percentOpen) {
canvas.scale(percentOpen, 1, 0, 0); }
}; }
package com.cn.slidingmenu.fragment; import com.cn.slidingmenu.fragment.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class Fragment1 extends Fragment { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//设置fragment的layout
View view = inflater.inflate(R.layout.text, null);
TextView tv = (TextView) view.findViewById(R.id.test);
tv.setText(Fragment1.class.getSimpleName());
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
} }
package com.cn.slidingmenu.fragment; import java.util.ArrayList;
import java.util.List;
import com.cn.slidingmenu.fragment.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class LeftFragment extends Fragment implements OnItemClickListener { private View rootView; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 设置fragment的layout
rootView = inflater.inflate(R.layout.activity_main, null);
return rootView;
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView lv = (ListView) rootView.findViewById(R.id.listview); List<String> list = initData(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, list); lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
} /**
* 初始化数据
* @return
*/
public List<String> initData() {
List<String> list = new ArrayList<String>();
list.add(Fragment1.class.getSimpleName());
list.add(Fragment2.class.getSimpleName());
list.add(Fragment3.class.getSimpleName());
list.add(Fragment4.class.getSimpleName());
list.add(Fragment5.class.getSimpleName());
return list;
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Fragment f = null;
switch (position) {
case 0:
f = new Fragment1();
break;
case 1:
f = new Fragment2();
break;
case 2:
f = new Fragment3();
break;
case 3:
f = new Fragment4();
break;
case 4:
f = new Fragment5();
break;
default:
f = new Fragment1();
break;
}
switchFragment(f);
} /**
* listview item 点击时切换fragment
* @param f
*/
private void switchFragment(Fragment f) {
if (getActivity() instanceof MainActivity) {
MainActivity activity = (MainActivity) getActivity();
activity.switchFragment(f);
}
} }
package com.cn.slidingmenu.fragment; import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class RightFragment extends Fragment { private View rootView; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_main, null);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView lv = (ListView) rootView.findViewById(R.id.listview);
List<String> list = initData();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
list); lv.setAdapter(adapter);
} /**
* 初始化数据
* @return
*/
public List<String> initData() {
List<String> list = new ArrayList<String>();
list.add(Fragment1.class.getSimpleName());
list.add(Fragment2.class.getSimpleName());
list.add(Fragment3.class.getSimpleName());
list.add(Fragment4.class.getSimpleName());
list.add(Fragment5.class.getSimpleName());
return list;
} }
DEMO下载地址:http://pan.baidu.com/s/1o6v7iX0
文件中三个包, SlidingMenuFragment21引用library,library引用actionbarsherlock。