去掉标题,全屏
动画
rlRoot= (RelativeLayout)findViewById(R.id.rl_root);
RotateAnimation animRotate=newRotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animRotate.setDuration(1000);
animRotate.setFillAfter(true);//保持状体
ScaleAnimation animScale=newScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animScale.setDuration(1000);
animScale.setFillAfter(true);
AlphaAnimation animAlpha=newAlphaAnimation(0, 1);
animAlpha.setDuration(1000);
animAlpha.setFillAfter(true);
AnimationSet animSet=newAnimationSet(true);
animSet.addAnimation(animRotate);
animSet.addAnimation(animAlpha);
animSet.addAnimation(animScale);
rlRoot.startAnimation(animSet);
animSet.setAnimationListener(newAnimationListener() {
@Override
publicvoidonAnimationStart(Animation animation) {
}
@Override
publicvoidonAnimationRepeat(Animation animation) {
}
@Override
publicvoidonAnimationEnd(Animation animation) {
startActivity(newIntent(getApplicationContext(), MainActivity.class));
finish();
}
});
库文件
打包不需要管库文件,自动打到项目里
引导页(ViewPage)
xml文件
拷贝全路径
关联viewPage源码
先remove
Addjar
勾上
源码路径
E:\adt-bundle-windows-x86\sdk\extras\android\support\v4
java代码
privateint[]mImageIds=newint[]{R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3};
privateViewPagermViewPage;
privateArrayListmImageViewList;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
//TODOAuto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
mViewPage= (ViewPager) findViewById(R.id.vp_pager);
mViewPage.setAdapter(newMyAdapter());
mImageViewList=newArrayList();
for(inti = 0; i
ImageView view=newImageView(this);
view.setBackgroundResource(mImageIds[i]);
mImageViewList.add(view);
}
}
classMyAdapterextendsPagerAdapter{
@Override
publicintgetCount() {
returnmImageIds.length;
}
@Override
publicbooleanisViewFromObject(View view, Object object) {
returnview==object;
}
@Override
publicObject instantiateItem(ViewGroup container,intposition) {
//ImageView view=new ImageView(getApplicationContext());
//view.setBackgroundResource(mImageIds[position]);
ImageView view=mImageViewList.get(position);
container.addView(view);
returnview;
}
@Override
publicvoiddestroyItem(ViewGroup container,intposition, Object object) {
container.removeView((View)object);
}
}
加点,加按钮
java代码
privateViewPagermViewPage;
privateLinearLayoutllContainer;
privateImageViewivRedPoint;
privateButtonbtStart;
privateint[]mImageIds=newint[]{R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3};
privateArrayListmImageViewList;
privateintmPointWith;//两个小圆点的宽度
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
//TODOAuto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
mViewPage= (ViewPager) findViewById(R.id.vp_pager);
llContainer=(LinearLayout)findViewById(R.id.ll_container);
ivRedPoint=(ImageView)findViewById(R.id.iv_red_point);
btStart=(Button)findViewById(R.id.bt_start);
btStart.setOnClickListener(this);
mImageViewList=newArrayList();
for(inti = 0; i
ImageView view=newImageView(this);
view.setBackgroundResource(mImageIds[i]);
mImageViewList.add(view);
ImageView pointView=newImageView(this);
pointView.setImageResource(R.drawable.shape_circle_default);
LinearLayout.LayoutParams params=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
if(i>0){
params.leftMargin=10;//从第二个点开始设置边距
}
pointView.setLayoutParams(params);
llContainer.addView(pointView);
}
mViewPage.setAdapter(newMyAdapter());
//页面绘制结束后,计算两个圆点的距离
ivRedPoint.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener() {
//layout方法执行结束之后(位置确定)
@Override
publicvoidonGlobalLayout() {
//移除监听
ivRedPoint.getViewTreeObserver().removeGlobalOnLayoutListener(this);
mPointWith=llContainer.getChildAt(1).getLeft()-llContainer.getChildAt(0).getLeft();
}
});
//页面滑动过程中回调
mViewPage.setOnPageChangeListener(newOnPageChangeListener() {
@Override
publicvoidonPageSelected(intposition) {
if(position==mImageIds.length-1){
btStart.setVisibility(View.VISIBLE);
}else
btStart.setVisibility(View.INVISIBLE);
}
@Override
publicvoidonPageScrolled(intposition,floatpositionOffset,
intpositionOffsetPixels) {
//计算当前小红点的左边距
intleftMargin=(int)(mPointWith*positionOffset+position*mPointWith);
//修改小红点的左边距
RelativeLayout.LayoutParams params=(LayoutParams)ivRedPoint.getLayoutParams();
params.leftMargin=leftMargin;
ivRedPoint.setLayoutParams(params);
}
@Override
publicvoidonPageScrollStateChanged(intstate) {
}
});
}
classMyAdapterextendsPagerAdapter{
@Override
publicintgetCount() {
returnmImageIds.length;
}
@Override
publicbooleanisViewFromObject(View view, Object object) {
returnview==object;
}
@Override
publicObject instantiateItem(ViewGroup container,intposition) {
//ImageView view=new ImageView(getApplicationContext());
//view.setBackgroundResource(mImageIds[position]);
ImageView view=mImageViewList.get(position);
container.addView(view);
returnview;
}
@Override
publicvoiddestroyItem(ViewGroup container,intposition, Object object) {
container.removeView((View)object);
}
}
@Override
publicvoidonClick(View v) {
//TODOAuto-generated method stub
switch(v.getId()) {
caseR.id.bt_start:
//PrefUtils.putBoolean("is_guide_show",true, this);
startActivity(newIntent(this, MainActivity.class));
finish();
break;
default:
break;
}
}
Xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/vp_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
>
android:id="@+id/iv_red_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shape_circle_selected"
/>
android:id="@+id/ll_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:id="@+id/bt_start"
android:visibility="invisible"
android:text="开始体验"
android:background="@drawable/btn_guide_selector"
android:textColor="@color/text_guide_selector"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
>
点击样式,状态选择器
SlidingMenu
java代码
/*
*开发流程:
* 1.引入SlidingMenu库
* 2.Activity继承自SlidingFragmentActivity
* 3.onCreate改为public
* 4.setBehindContentView,设置基本侧边栏效果
* 5.获取SlidingMenu,深层次定制
* */
publicclassMainActivityextendsSlidingFragmentActivity{
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.left_menu);
SlidingMenu slidingMenu=getSlidingMenu();
//全屏触摸
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
slidingMenu.setSecondaryMenu(R.layout.right_menu);
//修改侧边栏宽度
slidingMenu.setBehindOffset(100);//屏幕预留100个像素宽度
}
}
Fragement
继承自v4的,app是4.0后的版本,v4是任何版本都可以用
生命周期
Left_menu.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00"
android:id="@+id/fl_left_menu">
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是侧边栏"/>
Activity_main.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl_content">
BaseFragement类
/**
* 1.初始化布局initView
* 2.初始化initData
*@authorliangjun
*
*/
publicabstractclassBaseFragementextendsFragment {
publicActivitymActivity;
//Fragement被创建
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity= getActivity();
}
//初始化Fragement布局
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//TODOAuto-generated method stub
View view=initView();
returnview;
}
//activity创建结束
@Override
publicvoidonActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
/**
*初始化布局,子类必须实现
*@return
*/
publicabstractView initView();
/**
*初始化数据,子类可以不实现
*/
publicvoidinitData(){}
}
ContentMenuFragement类
publicclassContentMenuFragementextendsBaseFragement {
@Override
publicView initView() {
View view=View.inflate(mActivity, R.layout.fragement_left_menu,null);
returnview;
}
}
LeftMenuFragement类
publicclassLeftMenuFragementextendsBaseFragement {
@Override
publicView initView() {
View view=View.inflate(mActivity, R.layout.fragement_left_menu,null);
returnview;
}
}
MainActivity类
publicclassMainActivityextendsSlidingFragmentActivity {
StringTAG_LEFT_MENU="TAG_LEFT_MENU";
StringTAG_CONTENT="TAG_CONTENT";
@Override
publicvoidonCreate(Bundle savedInstanceState) {
//TODOAuto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.left_menu);
SlidingMenu slidingMenu=getSlidingMenu();
//全屏触摸
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//修改侧边栏宽度
slidingMenu.setBehindOffset(100);//屏幕预留100个像素宽度
initFragement();
}
privatevoidinitFragement() {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();//开启事务
//将帧布局替换为对应的Fragement
transaction.replace(R.id.fl_left_menu,newLeftMenuFragement(),TAG_LEFT_MENU);
transaction.replace(R.id.fl_content,newContentMenuFragement(),TAG_CONTENT);
transaction.commit();//提交事务
}
}
fragement_content.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f90"
android:orientation="vertical">
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是Fragement侧边栏"/>
fragement_left_menu.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f90"
android:orientation="vertical">
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是Fragement主页面"/>
效果
主页面底部菜单按钮 xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f90"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bottom_tab_bg"
android:orientation="horizontal"
android:gravity="center">
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/btn_home_selector"
android:text="首页"
/>
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/home"
android:text="首页"
/>
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/home"
android:text="首页"
/>
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/home"
android:text="首页"
/>
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/home"
android:text="首页"
/>
样式
"ButtonTabStyle">
"android:layout_width">wrap_content
"android:layout_height">wrap_content
"android:gravity">center
"android:layout_weight">1
"android:padding">5dp
"android:button">@null
"android:textColor">@color/text_button_tab_selector
XUtil
导入jar包
使用
@ViewInject(R.id.vp_pager)
privateViewPagermVierPager;
@Override
publicView initView() {
View view=View.inflate(mActivity, R.layout.fragement_content,null);
ViewUtils.inject(this,view);//注入view和事件
returnview;
}
ContentFragement页面
五个标签页的基类
/**
*五个标签页的基类
*@authorliangjun
*
*/
publicabstractclassBasePage {
publicActivitymActivity;
publicViewmRootView;
publicImageButtonbtnMenu;
publicFrameLayoutflContent;
publicTextViewtvTitle;
publicBasePage(Activity activity){
mActivity=activity;
initView();
}
publicvoidinitView() {
mRootView= View.inflate(mActivity, R.layout.base_paper,null);
tvTitle= (TextView)mRootView.findViewById(R.id.tv_title);
btnMenu=(ImageButton)mRootView.findViewById(R.id.btn_menu);
flContent=(FrameLayout)mRootView.findViewById(R.id.fl_content2);
}
/**
*初始化数据
*/
publicabstractvoidinitData();
}
举例首页
publicclassHomePagerextendsBasePage {
publicHomePager(Activity activity) {
super(activity);
}
@Override
publicvoidinitData() {
tvTitle.setText("智慧上海");
TextView view=newTextView(mActivity);
view.setText("首页");
view.setTextColor(Color.RED);
view.setTextSize(22);
view.setGravity(Gravity.CENTER);
flContent.addView(view);
}
}
ContentMenuFragement类
publicclassContentMenuFragementextendsBaseFragement {
@ViewInject(R.id.vp_pager)
privateViewPagermVierPager;
privateArrayListmPagers;
@Override
publicView initView() {
View view=View.inflate(mActivity, R.layout.fragement_content,null);
ViewUtils.inject(this,view);//注入view和事件
returnview;
}
@Override
publicvoidinitData() {
super.initData();
//初始化5个标签页面
mPagers=newArrayList();
mPagers.add(newHomePager(mActivity));
mPagers.add(newNewCenterPager(mActivity));
mPagers.add(newSmartServicePager(mActivity));
mPagers.add(newGovAffairsPager(mActivity));
mPagers.add(newSettingPager(mActivity));
mVierPager.setAdapter(newContentAdapter());
}
classContentAdapterextendsPagerAdapter{
@Override
publicintgetCount() {
returnmPagers.size();
}
@Override
publicbooleanisViewFromObject(View arg0, Object arg1) {
returnarg0==arg1;
}
@Override
publicObject instantiateItem(ViewGroup container,intposition) {
BasePage pager=mPagers.get(position);
container.addView(pager.mRootView);//将页面添加到容器中
pager.initData();//初始化数据
returnpager.mRootView;
}
}
}
BaseFragement类
/**
* 1.初始化布局initView
* 2.初始化initData
*@authorliangjun
*
*/
publicabstractclassBaseFragementextendsFragment {
publicActivitymActivity;
//Fragement被创建
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity= getActivity();
}
//初始化Fragement布局
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//TODOAuto-generated method stub
View view=initView();
returnview;
}
//activity创建结束
@Override
publicvoidonActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
/**
*初始化布局,子类必须实现
*@return
*/
publicabstractView initView();
/**
*初始化数据,子类可以不实现
*/
publicvoidinitData(){}
}
总结布局框架
继承自v4的,app是4.0后的版本,v4是任何版本都可以用
生命周期
Left_menu.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00"
android:id="@+id/fl_left_menu">
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是侧边栏"/>
Activity_main.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl_content">
BaseFragement类
/**
* 1.初始化布局initView
* 2.初始化initData
*@authorliangjun
*
*/
publicabstractclassBaseFragementextendsFragment {
publicActivitymActivity;
//Fragement被创建
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity= getActivity();
}
//初始化Fragement布局
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//TODOAuto-generated method stub
View view=initView();
returnview;
}
//activity创建结束
@Override
publicvoidonActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
/**
*初始化布局,子类必须实现
*@return
*/
publicabstractView initView();
/**
*初始化数据,子类可以不实现
*/
publicvoidinitData(){}
}
ContentMenuFragement类
publicclassContentMenuFragementextendsBaseFragement {
@Override
publicView initView() {
View view=View.inflate(mActivity, R.layout.fragement_left_menu,null);
returnview;
}
}
LeftMenuFragement类
publicclassLeftMenuFragementextendsBaseFragement {
@Override
publicView initView() {
View view=View.inflate(mActivity, R.layout.fragement_left_menu,null);
returnview;
}
}
MainActivity类
publicclassMainActivityextendsSlidingFragmentActivity {
StringTAG_LEFT_MENU="TAG_LEFT_MENU";
StringTAG_CONTENT="TAG_CONTENT";
@Override
publicvoidonCreate(Bundle savedInstanceState) {
//TODOAuto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.left_menu);
SlidingMenu slidingMenu=getSlidingMenu();
//全屏触摸
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//修改侧边栏宽度
slidingMenu.setBehindOffset(100);//屏幕预留100个像素宽度
initFragement();
}
privatevoidinitFragement() {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();//开启事务
//将帧布局替换为对应的Fragement
transaction.replace(R.id.fl_left_menu,newLeftMenuFragement(),TAG_LEFT_MENU);
transaction.replace(R.id.fl_content,newContentMenuFragement(),TAG_CONTENT);
transaction.commit();//提交事务
}
}
fragement_content.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f90"
android:orientation="vertical">
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是Fragement侧边栏"/>
fragement_left_menu.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f90"
android:orientation="vertical">
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是Fragement主页面"/>
效果
主页面底部菜单按钮 xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f90"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bottom_tab_bg"
android:orientation="horizontal"
android:gravity="center">
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/btn_home_selector"
android:text="首页"
/>
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/home"
android:text="首页"
/>
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/home"
android:text="首页"
/>
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/home"
android:text="首页"
/>
style="@style/ButtonTabStyle"
android:drawableTop="@drawable/home"
android:text="首页"
/>
样式
"ButtonTabStyle">
"android:layout_width">wrap_content
"android:layout_height">wrap_content
"android:gravity">center
"android:layout_weight">1
"android:padding">5dp
"android:button">@null
"android:textColor">@color/text_button_tab_selector
XUtil
导入jar包
使用
@ViewInject(R.id.vp_pager)
privateViewPagermVierPager;
@Override
publicView initView() {
View view=View.inflate(mActivity, R.layout.fragement_content,null);
ViewUtils.inject(this,view);//注入view和事件
returnview;
}
ContentFragement页面
五个标签页的基类
/**
*五个标签页的基类
*@authorliangjun
*
*/
publicabstractclassBasePage {
publicActivitymActivity;
publicViewmRootView;
publicImageButtonbtnMenu;
publicFrameLayoutflContent;
publicTextViewtvTitle;
publicBasePage(Activity activity){
mActivity=activity;
initView();
}
publicvoidinitView() {
mRootView= View.inflate(mActivity, R.layout.base_paper,null);
tvTitle= (TextView)mRootView.findViewById(R.id.tv_title);
btnMenu=(ImageButton)mRootView.findViewById(R.id.btn_menu);
flContent=(FrameLayout)mRootView.findViewById(R.id.fl_content2);
}
/**
*初始化数据
*/
publicabstractvoidinitData();
}
举例首页
publicclassHomePagerextendsBasePage {
publicHomePager(Activity activity) {
super(activity);
}
@Override
publicvoidinitData() {
tvTitle.setText("智慧上海");
TextView view=newTextView(mActivity);
view.setText("首页");
view.setTextColor(Color.RED);
view.setTextSize(22);
view.setGravity(Gravity.CENTER);
flContent.addView(view);
}
}
ContentMenuFragement类
publicclassContentMenuFragementextendsBaseFragement {
@ViewInject(R.id.vp_pager)
privateViewPagermVierPager;
privateArrayListmPagers;
@Override
publicView initView() {
View view=View.inflate(mActivity, R.layout.fragement_content,null);
ViewUtils.inject(this,view);//注入view和事件
returnview;
}
@Override
publicvoidinitData() {
super.initData();
//初始化5个标签页面
mPagers=newArrayList();
mPagers.add(newHomePager(mActivity));
mPagers.add(newNewCenterPager(mActivity));
mPagers.add(newSmartServicePager(mActivity));
mPagers.add(newGovAffairsPager(mActivity));
mPagers.add(newSettingPager(mActivity));
mVierPager.setAdapter(newContentAdapter());
}
classContentAdapterextendsPagerAdapter{
@Override
publicintgetCount() {
returnmPagers.size();
}
@Override
publicbooleanisViewFromObject(View arg0, Object arg1) {
returnarg0==arg1;
}
@Override
publicObject instantiateItem(ViewGroup container,intposition) {
BasePage pager=mPagers.get(position);
container.addView(pager.mRootView);//将页面添加到容器中
pager.initData();//初始化数据
returnpager.mRootView;
}
}
}
BaseFragement类
/**
* 1.初始化布局initView
* 2.初始化initData
*@authorliangjun
*
*/
publicabstractclassBaseFragementextendsFragment {
publicActivitymActivity;
//Fragement被创建
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity= getActivity();
}
//初始化Fragement布局
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//TODOAuto-generated method stub
View view=initView();
returnview;
}
//activity创建结束
@Override
publicvoidonActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
/**
*初始化布局,子类必须实现
*@return
*/
publicabstractView initView();
/**
*初始化数据,子类可以不实现
*/
publicvoidinitData(){}
}
总结布局框架