采用SlideMenu实现侧边栏的效果:
其中
setContentView是设置主背景的布局
setBehindContentView是设置左边菜单的布局
setSecondaryMenu是设置右边的布局
setShadowWidth是设置阴影的宽度
setBehindWidth是设置有效的拉出宽度
setMode是设置开启左右两边的菜单
采用ViewPagerIndictor实现滑动的导航栏和页面布局
其中采用TabPageIndicator做可滑动导航栏
ViewPager做滑动的布局
首先要设置导航栏的样式
<style name="HomeUpTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item> </style> <style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator"> <item name="android:dividerPadding">10dp</item> <item name="android:paddingLeft">20dp</item> <item name="android:paddingRight">20dp</item> <item name="android:paddingTop">10dp</item> <item name="android:paddingBottom">10dp</item> <item name="android:textSize">10pt</item> <item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item> <item name="android:textColor">#000000</item> <item name="android:fadingEdge">horizontal</item> <item name="android:fadingEdgeLength">8dp</item> <item name="android:background">@drawable/selectitle</item> <item name="android:gravity">center</item> </style>在代码中
布局适配器PagerAdapter重写instantiateItem方法
碎片适配器FragmentStatePagerAdapter重写Fragment getItem()的方法
最后把两者关联起来
indicatorView.setViewPager(pager);
完整代码:
public class MainAct extends SlidingActivity { SlidingMenu slidingMenu; TabPageIndicator indicatorView; ViewPager pager; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setBehindContentView(R.layout.slide_behind); setContentView(R.layout.slide_main); slidingMenu = getSlidingMenu(); slidingMenu.setBehindOffset(300); slidingMenu.setBehindWidth(400); slidingMenu.setSecondaryMenu(R.layout.slide_second); // slidingMenu.setTouchModeAbove(SlidingMenu.LEFT_RIGHT); slidingMenu.setShadowWidth(100); slidingMenu.setMode(SlidingMenu.LEFT_RIGHT); indicatorView = (TabPageIndicator) findViewById(R.id.indicator); pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new Myadapter()); pager.setOffscreenPageLimit(8); indicatorView.setOnTabReselectedListener(new OnTabReselectedListener() { @Override public void onTabReselected(int position) { Toast.makeText(getApplicationContext(), position + "", 3000) .show(); // TODO Auto-generated method stub pager.setCurrentItem(position); } }); pager.setCurrentItem(0); indicatorView.setViewPager(pager); indicatorView.setClickable(true); } class Myadapter extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return 8; } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return "--" + position + "--"; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub ((ViewPager) container).removeViewAt(position); super.destroyItem(container, position, object); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub ImageView imageView = new ImageView(getApplicationContext()); imageView.setImageBitmap(BitmapFactory.decodeResource( getResources(), R.drawable.flower)); container.addView(imageView); return imageView; // return super.instantiateItem(container, position); } } }