側滑+viewpager的效果首先要引用Slidingmenu的項目,而後對主Activity進行相應的修改,具體代碼以下:
ide
具體代碼以下:
佈局
主界面:
this
/**spa
* @描述:主界面.net
* @做者:劉倩get
* @時間:2014-11-13 下午4:25:40it
*/io
public class MainActivity extends SlidingFragmentActivity {class
/** 聲明主界面的Fragment */List
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