侧滑+viewpager的效果首先要引用Slidingmenu的项目,然后对主Activity进行相应的修改,具体代码如下:
具体代码如下:
主界面:
/**
* @描述:主界面
* @作者:刘倩
* @时间:2014-11-13 下午4:25:40
*/
public class MainActivity extends SlidingFragmentActivity {
/** 声明主界面的Fragment */
private Fragment mContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置为无窗口的形式
requestWindowFeature(Window.FEATURE_NO_TITLE);
initView(savedInstanceState);
}
/**
* 初始化视图
*
* @param savedInstanceState
*/
private void initView(Bundle savedInstanceState) {
setContentView(R.layout.layout_main);
if (findViewById(R.id.set_frame) == null) {
setBehindContentView(R.layout.set_frame);
getSlidingMenu().setSlidingEnabled(true);
getSlidingMenu()
.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
} else {
/* 添加虚拟的视图 */
View v = new View(this);
setBehindContentView(v);
getSlidingMenu().setSlidingEnabled(false);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
}
/* 设置覆盖在最上面的视图 */
if (savedInstanceState != null) {
mContent = getSupportFragmentManager().getFragment(
savedInstanceState, "mContent");
}
if (mContent == null) {
mContent = new MainFragment();
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_content_frame, mContent).commit();
/* 设置被盖在下面的视图 */
getSupportFragmentManager().beginTransaction()
.replace(R.id.set_frame, new SetFragment()).commit();
/* 自定义SlidingMenu */
SlidingMenu sm = getSlidingMenu();
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeEnabled(false);
sm.setBehindScrollScale(0.25f);
sm.setFadeDegree(0.25f);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}
}
主要对MainFragment做修改:
/**
* @描述:主界面,主要是书架功能
* @作者:刘倩
* @时间:2014-12-25 下午5:31:10
*/
public class MainFragment extends Fragment {
/** 声明ViewPager */
private ViewPager mPager;
/** 声明存放Fragment的集合 */
private ArrayList<Fragment> fragmentsList;
/** 书架的Fragment */
Fragment storeFragment;
/** 书城的Fragment */
Fragment bookshelfFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return initView(inflater);
}
/**
* 初始化视图
*
* @return
*/
private View initView(LayoutInflater inflater) {
View mView = inflater.inflate(R.layout.layout_main_fragment, null);
InitViewPager(mView);
return mView;
}
private void InitViewPager(View parentView) {
mPager = (ViewPager) parentView.findViewById(R.id.main_vPager);
fragmentsList = new ArrayList<Fragment>();
bookshelfFragment = new BookshelfFragment();
storeFragment = new StoreFragment();
fragmentsList.add(bookshelfFragment);
fragmentsList.add(storeFragment);
mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(),
fragmentsList));
mPager.setOnPageChangeListener(new MyOnPageChangeListener());
mPager.setCurrentItem(0);
}
public class MyOnPageChangeListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) { }
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
break;
default:
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
break;
}
}
}
private SlidingMenu getSlidingMenu() {
return ((MainActivity)getActivity()).getSlidingMenu();
}
}
第三步就是对BookshelfFragment和StoreFragment进行布局了。
具体的下载地址如下:
http://download.csdn.net/detail/u010820846/8314977