packagecom.ns.pager;importjava.util.ArrayList;importandroid.app.Activity;importandroid.os.Bundle;importandroid.support.v4.view.PagerAdapter;importandroid.support.v4.view.PagerTitleStrip;importandroid.support.v4.view.ViewPager;importandroid.view.LayoutInflater;importandroid.view.View;publicclassPagerTitleDemoActivityextendsActivity{/** Called when the activity is first created. */privateViewPagermViewPager;privatePagerTitleStripmPagerTitleStrip;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);mViewPager=(ViewPager)findViewById(R.id.viewpager);mPagerTitleStrip=(PagerTitleStrip)findViewById(R.id.pagertitle);//将要分页显示的View装入数组中LayoutInflatermLi=LayoutInflater.from(this);Viewview1=mLi.inflate(R.layout.view1,null);Viewview2=mLi.inflate(R.layout.view2,null);Viewview3=mLi.inflate(R.layout.view3,null);//每个页面的Title数据finalArrayList<View>views=newArrayList<View>();views.add(view1);views.add(view2);views.add(view3);finalArrayList<String>titles=newArrayList<String>();titles.add("tab1");titles.add("tab2");titles.add("tab3");//填充ViewPager的数据适配器PagerAdaptermPagerAdapter=newPagerAdapter(){@OverridepublicbooleanisViewFromObject(Viewarg0,Objectarg1){returnarg0==arg1;}@OverridepublicintgetCount(){returnviews.size();}@OverridepublicvoiddestroyItem(Viewcontainer,intposition,Objectobject){((ViewPager)container).removeView(views.get(position));}@OverridepublicCharSequencegetPageTitle(intposition){returntitles.get(position);}@OverridepublicObjectinstantiateItem(Viewcontainer,intposition){((ViewPager)container).addView(views.get(position));returnviews.get(position);}};mViewPager.setAdapter(mPagerAdapter);}}
packagecom.notice.viewpagerd;importjava.util.List;importandroid.os.Parcelable;importandroid.support.v4.view.PagerAdapter;importandroid.support.v4.view.ViewPager;importandroid.view.View;publicclassViewPagerAdapterextendsPagerAdapter{//界面列表privateList<View>views;publicViewPagerAdapter(List<View>views){this.views=views;}//销毁arg1位置的界面@OverridepublicvoiddestroyItem(Viewarg0,intarg1,Objectarg2){((ViewPager)arg0).removeView(views.get(arg1));}@OverridepublicvoidfinishUpdate(Viewarg0){// TODO Auto-generated method stub}//获得当前界面数@OverridepublicintgetCount(){if(views!=null){returnviews.size();}return0;}//初始化arg1位置的界面@OverridepublicObjectinstantiateItem(Viewarg0,intarg1){((ViewPager)arg0).addView(views.get(arg1),0);returnviews.get(arg1);}//判断是否由对象生成界面@OverridepublicbooleanisViewFromObject(Viewarg0,Objectarg1){return(arg0==arg1);}@OverridepublicvoidrestoreState(Parcelablearg0,ClassLoaderarg1){// TODO Auto-generated method stub}@OverridepublicParcelablesaveState(){// TODO Auto-generated method stubreturnnull;}@OverridepublicvoidstartUpdate(Viewarg0){// TODO Auto-generated method stub}}
packagecom.notice.viewpagerd;importjava.util.ArrayList;importjava.util.List;importandroid.app.Activity;importandroid.os.Bundle;importandroid.support.v4.view.ViewPager;importandroid.support.v4.view.ViewPager.OnPageChangeListener;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.ImageView;importandroid.widget.LinearLayout;publicclassViewPagerDemoActivityextendsActivityimplementsOnClickListener,OnPageChangeListener{privateViewPagervp;privateViewPagerAdaptervpAdapter;privateList<View>views;//引导图片资源privatestaticfinalint[]pics={R.drawable.whatsnew_00,R.drawable.whatsnew_01,R.drawable.whatsnew_02,R.drawable.whatsnew_03};//底部小店图片privateImageView[]dots;//记录当前选中位置privateintcurrentIndex;/** Called when the activity is first created. */@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);views=newArrayList<View>();LinearLayout.LayoutParamsmParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);//初始化引导图片列表for(inti=0;i<pics.length;i++){ImageViewiv=newImageView(this);iv.setLayoutParams(mParams);iv.setImageResource(pics[i]);views.add(iv);}vp=(ViewPager)findViewById(R.id.viewpager);//初始化AdaptervpAdapter=newViewPagerAdapter(views);vp.setAdapter(vpAdapter);//绑定回调vp.setOnPageChangeListener(this);//初始化底部小点initDots();}privatevoidinitDots(){LinearLayoutll=(LinearLayout)findViewById(R.id.ll);dots=newImageView[pics.length];//循环取得小点图片for(inti=0;i<pics.length;i++){dots[i]=(ImageView)ll.getChildAt(i);dots[i].setEnabled(true);//都设为灰色dots[i].setOnClickListener(this);dots[i].setTag(i);//设置位置tag,方便取出与当前位置对应}currentIndex=0;dots[currentIndex].setEnabled(false);//设置为白色,即选中状态}/** *设置当前的引导页 */privatevoidsetCurView(intposition){if(position<0||position>=pics.length){return;}vp.setCurrentItem(position);}/** *这只当前引导小点的选中 */privatevoidsetCurDot(intpositon){if(positon<0||positon>pics.length-1||currentIndex==positon){return;}dots[positon].setEnabled(false);dots[currentIndex].setEnabled(true);currentIndex=positon;}//当滑动状态改变时调用@OverridepublicvoidonPageScrollStateChanged(intarg0){// TODO Auto-generated method stub}//当当前页面被滑动时调用@OverridepublicvoidonPageScrolled(intarg0,floatarg1,intarg2){// TODO Auto-generated method stub}//当新的页面被选中时调用@OverridepublicvoidonPageSelected(intarg0){//设置底部小点选中状态setCurDot(arg0);}@OverridepublicvoidonClick(Viewv){intposition=(Integer)v.getTag();setCurView(position);setCurDot(position);}}