开源组件:DEMO学习-DWinterTabDemo:自定义tab+viewpager
效果图:
demo源码:
publicclassMainActivityextendsActivity{//ViewPager是googleSDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。//android-support-v4.jarprivateViewPagermPager;//页卡内容privateList<View>listViews;//Tab页面列表privateImageViewcursor;//动画图片privateTextViewt1,t2,t3;//页卡头标privateintoffset=0;//动画图片偏移量privateintcurrIndex=0;//当前页卡编号privateintbmpW;//动画图片宽度@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);InitImageView();InitTextView();InitViewPager();}/***初始化头标*/privatevoidInitTextView(){t1=(TextView)findViewById(R.id.text1);t2=(TextView)findViewById(R.id.text2);t3=(TextView)findViewById(R.id.text3);t1.setOnClickListener(newMyOnClickListener(0));t2.setOnClickListener(newMyOnClickListener(1));t3.setOnClickListener(newMyOnClickListener(2));}/***初始化ViewPager*/privatevoidInitViewPager(){mPager=(ViewPager)findViewById(R.id.vPager);listViews=newArrayList<View>();LayoutInflatermInflater=getLayoutInflater();listViews.add(mInflater.inflate(R.layout.lay1,null));listViews.add(mInflater.inflate(R.layout.lay2,null));listViews.add(mInflater.inflate(R.layout.lay3,null));mPager.setAdapter(newMyPagerAdapter(listViews));mPager.setCurrentItem(0);mPager.setOnPageChangeListener(newMyOnPageChangeListener());}/***初始化动画*/privatevoidInitImageView(){cursor=(ImageView)findViewById(R.id.cursor);bmpW=BitmapFactory.decodeResource(getResources(),R.drawable.a).getWidth();//获取图片宽度DisplayMetricsdm=newDisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);intscreenW=dm.widthPixels;//获取分辨率宽度offset=(screenW/3-bmpW)/2;//计算偏移量Matrixmatrix=newMatrix();matrix.postTranslate(offset,0);cursor.setImageMatrix(matrix);//设置动画初始位置}/***ViewPager适配器*/publicclassMyPagerAdapterextendsPagerAdapter{publicList<View>mListViews;publicMyPagerAdapter(List<View>mListViews){this.mListViews=mListViews;}@OverridepublicvoiddestroyItem(Viewarg0,intarg1,Objectarg2){((ViewPager)arg0).removeView(mListViews.get(arg1));}@OverridepublicvoidfinishUpdate(Viewarg0){}@OverridepublicintgetCount(){returnmListViews.size();}@OverridepublicObjectinstantiateItem(Viewarg0,intarg1){((ViewPager)arg0).addView(mListViews.get(arg1),0);returnmListViews.get(arg1);}@OverridepublicbooleanisViewFromObject(Viewarg0,Objectarg1){returnarg0==(arg1);}@OverridepublicvoidrestoreState(Parcelablearg0,ClassLoaderarg1){}@OverridepublicParcelablesaveState(){returnnull;}@OverridepublicvoidstartUpdate(Viewarg0){}}/***头标点击监听*/publicclassMyOnClickListenerimplementsView.OnClickListener{privateintindex=0;publicMyOnClickListener(inti){index=i;}@OverridepublicvoidonClick(Viewv){mPager.setCurrentItem(index);}};/***页卡切换监听*/publicclassMyOnPageChangeListenerimplementsOnPageChangeListener{intone=offset*2+bmpW;//页卡1->页卡2偏移量inttwo=one*2;//页卡1->页卡3偏移量@OverridepublicvoidonPageSelected(intarg0){Animationanimation=null;switch(arg0){case0:if(currIndex==1){animation=newTranslateAnimation(one,0,0,0);}elseif(currIndex==2){animation=newTranslateAnimation(two,0,0,0);}break;case1:if(currIndex==0){animation=newTranslateAnimation(offset,one,0,0);}elseif(currIndex==2){animation=newTranslateAnimation(two,one,0,0);}break;case2:if(currIndex==0){animation=newTranslateAnimation(offset,two,0,0);}elseif(currIndex==1){animation=newTranslateAnimation(one,two,0,0);}break;}currIndex=arg0;animation.setFillAfter(true);//True:图片停在动画结束位置animation.setDuration(300);cursor.startAnimation(animation);}@OverridepublicvoidonPageScrolled(intarg0,floatarg1,intarg2){}@OverridepublicvoidonPageScrollStateChanged(intarg0){}}}
其他方法:
1.ViewPager使用
http://zhangfy068.iteye.com/blog/1819979
2.页卡实现详解:Android ViewPager使用详解
http://blog.csdn.net/wangjinyu501/article/details/8169924
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。