Application of the market, the Top Game category tab games and software classification, is implemented with ViewPage.
The specific implementation code is as follows.
public class Mytabview extends LinearLayout {/** the context. */private context context;/** the M tab selector. */private Runnable Mtabselector; /** the M listener. */Private Viewpager.onpagechangelistener Mlistener; /** The M Max tab width. */public int mmaxtabwidth; /** The M selected tab index. */private int mselectedtabindex; The background of the/** tab. */private int tabbackgroundresource =-1; /** the text size of the tab. */private int tabtextsize = 30;/** Tab's text color. */private int tabtextcolor = color.black;/** Tab's selected text color. */private int tabselectcolor = color.black;/** tab's linear layout. */private linearlayout mtablayout = null;/** tab of the linear layout parent. */private Horizontalscrollview Mtabscrollview = null;/** the M view Pager. */private myviewpage mviewpager;/** tab text. */private list<string> tabitemtextlist = null;/** tab's icon. */private list<drawable> tabitemdrawablelist = view of the null;/** content. */private arraylist<fragment> pageritemlist = List of null;/** tabs. */privateArraylist<textview> Tabitemlist = Adapter for null;/** content area. */private Abfragmentpageradapter mfragmentpageradapter = null; /** The M tab click Listener. */private Onclicklistener Mtabclicklistener = new Onclicklistener () {public void OnClick (view view) {A Btabitemview Tabview = (abtabitemview) view; Mviewpager.setcurrentitem (Tabview.getindex ()); } }; /** * Instantiates a new AB sliding tab view. * * @param context the context * * Public abslidingtabview (Context context) {This (context, NULL); }/** * Instantiates a new AB sliding tab view. * * @param context the context * @param attrs the attrs * * Public <span style= "Font-family:arial, Helve TICA, Sans-serif; " >mytabview </span><span style= "font-family:arial, Helvetica, Sans-serif;" > (Context context, AttributeSet Attrs) {</span> super (context, attrs); This.context = context; This.setOrientation (linearlayout.vertical); This.setbackgroundcolor (Color.rgb (255, 255, 255)); Mtabscrollview = new Horizontalscrollview (context); mtabscrollview.sethorizontalscrollbarenabled (false); Mtabscrollview.setsmoothscrollingenabled (true); mtablayout = new LinearLayout (context); Mtablayout.setorientation ( Linearlayout.horizontal); mtablayout.setgravity (gravity.center);//mtablayout is the content width Mtabscrollview.addview ( Mtablayout, New Viewgroup.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT)); This.addview (mtabscrollview,new viewgroup.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); The adaptation of the content's view Mviewpager = new Myviewpage (context); Manually created Viewpager, you must call the SetId () method to set an ID Mviewpager.setid (1985); Pageritemlist = new arraylist<fragment> (); Define tab Bar Tabitemlist = new arraylist<textview> (); Tabitemtextlist = new arraylist<string> (); Tabitemdrawablelist = new arraylist<drawable> (); } public void Initchilldmanager (Fragmentmanager mfragmentmanager) {//required must be an instance of fragmentactivity, if (Mfragmentman Ager==null) {//If the above one is fragment please pass, Childsupport come over mfragmentmanager= ((fragmentactivity) this.context). Getsupportfragmentmanager (); } mfragmentpageradapter = new Abfragmentpageradapter (Mfragmentmanager, pageritemlist); Mviewpager.setadapter (Mfragmentpageradapter); Mviewpager.setonpagechangelistener (New Myonpagechangelistener ()); Mviewpager.setoffscreenpagelimit (3); This.addview (mviewpager,new linearlayout.layoutparams (layoutparams.fill_parent, LayoutParams.FILL_PARENT)); }/** * The Listener interface for receiving Myonpagechange events. * The class is interested in processing a myonpagechange * Event implements this interface, and the object create D * With that class are registered with a component using the * component ' s <CODE>ADDMYONPAGECHANGELISTENER&L T;code> method. When * the MyonpagechangE event occurs, that the object ' s appropriate * method is invoked. * * @see Myonpagechangeevent */public class Myonpagechangelistener implements onpagechangelistener{/* (Non-jav ADOc) * @see android.support.v4.view.viewpager.onpagechangelistener#onpagescrollstatechanged (int) */@ overridepublic void onpagescrollstatechanged (int arg0) {if (Mlistener! = null) {Mlistener.onpagescrollstatecha Nged (arg0); }}/* (non-javadoc) * @see android.support.v4.view.viewpager.onpagechangelistener#onpagescrolled (int, float, int) */@ overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {if (Mlistener! = null) {Mlistener.onpagesc Rolled (arg0, arg1, arg2); }}/* (non-javadoc) * @see android.support.v4.view.viewpager.onpagechangelistener#onpageselected (int) */@ overridepublic void onpageselected (int arg0) {setcurrentitem (arg0); if (Mlistener! = null) {mlistener.onpageselected (arg0); }}}/* (non-javadoc) * @see Android.widget.linearlayout#onmeasure (int, int) */@Override public void onmeasure (int widthmeasurespec, int heightme ASURESPEC) {final int widthmode = Measurespec.getmode (Widthmeasurespec); Final Boolean lockedexpanded = Widthmode = = measurespec.exactly; Mtabscrollview.setfillviewport (lockedexpanded); Final int childCount = Mtablayout.getchildcount (); if (ChildCount > 1 && (widthmode = = measurespec.exactly | | widthmode = = measurespec.at_most)) {if ( ChildCount > 2) {mmaxtabwidth = (int) (Measurespec.getsize (widthmeasurespec) * 0.4f); } else {mmaxtabwidth = Measurespec.getsize (widthmeasurespec)/2; }} else {mmaxtabwidth =-1; } final int oldwidth = Getmeasuredwidth (); Super.onmeasure (Widthmeasurespec, Heightmeasurespec); Final int newwidth = Getmeasuredwidth (); if (lockedexpanded && oldwidth! = newwidth) { Recenter The tab display if we ' re at a new (scrollable) size. Setcurrentitem (Mselectedtabindex); }}/** * scrolling animation * * @param position the position */private void Animatetotab (final int position) {final View Tabview = mtablayout.getchildat (position); if (mtabselector! = null) {//The tab selected earlier is not null to remove removecallbacks (mtabselector); } mtabselector = new Runnable () {//tab scrolling effect public void run () {final int scrollpos = tab View.getleft ()-(GetWidth ()-tabview.getwidth ())/2; Mtabscrollview.smoothscrollto (scrollpos, 0); Mtabselector = null; } }; Post (Mtabselector); }/* (non-javadoc) * @see Android.view.view#onattachedtowindow () */@Override public void Onattachedtowin Dow () {Super.onattachedtowindow (); if (mtabselector! = null) {//re-post the selector we saved Post (Mtabselector); } */* (non-javadoc) * @see Android.view.view#ondetachedfromwindow () */@Override public void Ondetach Edfromwindow () {Super.ondetachedfromwindow (); if (mtabselector! = null) {removecallbacks (mtabselector); }}/** * Description: Create a tab. * * @param text The text * @param index the index */private void AddTab (String text, int index) {ADDTA B (Text,index,null); }/** * Description: Create a tab. * * @param text The text * @param index the index * @param top of the top */private void AddTab (String te XT, int index,drawable top) {Abtabitemview Tabview = new Abtabitemview (this.context); if (tabbackgroundresource!=-1) {tabview.settabbackgroundresource (Tabbackgroundresource); } if (Top!=null) {tabview.settabcompounddrawables (null, top, NULL, NULL); } tabview.settabtextcolor (Tabtextcolor); Tabview.settabtextsize (tAbtextsize); Tabview.init (Index,text); Tabitemlist.add (Tabview.gettextview ()); Tabview.setonclicklistener (Mtabclicklistener); Mtablayout.addview (Tabview, New Layoutparams (0,layoutparams.match_parent,1)); }/** * Description: Tab has a change refresh. */public void notifytabdatasetchanged () {mtablayout.removeallviews (); Tabitemlist.clear (); Final int count = Mfragmentpageradapter.getcount (); for (int i = 0; i < count; i++) {if (Tabitemdrawablelist.size () >0) {AddTab (Tabitemtextlist.get (i), i,t Abitemdrawablelist.get (i)); }else{AddTab (Tabitemtextlist.get (i), i); }} if (Mselectedtabindex > Count) {mselectedtabindex = count-1; } setcurrentitem (Mselectedtabindex); Requestlayout (); }/** * Description: Sets which one is displayed. * * @param item The new current item */public void Setcurrentitem (int item) {if (Mviewpager = = NULL{throw new IllegalStateException ("Viewpager have not been bound."); } Mselectedtabindex = Item; Final int tabcount = Mtablayout.getchildcount (); for (int i = 0; i < Tabcount; i++) {final Abtabitemview child = (Abtabitemview) mtablayout.getchildat (i); Final Boolean isSelected = (i = = Item); Child.setselected (isSelected); if (isSelected) {child.settabtextcolor (tabselectcolor); Animatetotab (item); }else{Child.settabtextcolor (Tabtextcolor); }}}/** * Description: Set an external listener. * * @param listener the new on page change listener */public void Setonpagechangelistener (Viewpager.onpagechan Gelistener listener) {Mlistener = listener; }/** * Description: Sets the color of the tab text. * * @param tabcolor the new tab text color */public void settabtextcolor (int tabcolor) {this.tabtextcolor = Tabcolor;} /** * Description: Sets the selected color. * * @param tabcolor the new tab select Color */public void settabselectcolor (int tabcolor) {this.tabselectcolor = Tabcolor;} /** * Description: Sets the text size. * * @param tabtextsize the new tab text size */public void settabtextsize (int tabtextsize) {this.tabtextsize = Tabtextsize ;} /** * Description: Sets the background selector for a single tab. * * @param resid the new tab background resource */public void Settabbackgroundresource (int resid) {Tabbackgroundresou Rce = Resid; }/** * Description: Sets the background of the tab. * * @param resid the new tab layout background resource */public void Settablayoutbackgroundresource (int resid) {This.mtab Layout.setbackgroundresource (RESID);} /** * Description: Adds a set of content and tab. * * @param tabtexts the tab texts * @param fragments the Fragments */public void Additemviews (list<string> tabtexts, List<fragment> fragments) {Tabitemtextlist.addall (tabtexts);p ageritemlist.addall (fragments); Mfragmentpageradapter.notifydatasetchanged (); notifytabdatasetchanged ();} /** * Description: Adds a set of content and tab. * * @param tabtexts the tab texts * @param fragments the fragments * @param drawables the drawables */public void Additemviews (list<string> tabtexts,list<fragment> fragments,List<Drawable > Drawables) {tabitemtextlist.addall (tabtexts);p ageritemlist.addall (fragments); Tabitemdrawablelist.addall ( Drawables); mfragmentpageradapter.notifydatasetchanged (); notifytabdatasetchanged ();} /** * Description: Add a content with tab. * * @param tabtext the tab text * @param fragment the Fragment */public void Additemview (String tabtext,fragment fragment) {Tabitemtextlist.add (Tabtext);p ageritemlist.add (fragment); mfragmentpageradapter.notifydatasetchanged (); Notifytabdatasetchanged ();} /** * Description: Add a content with tab. * * @param tabtext the tab text * @param fragment the fragment * @param drawable the drawable */public void Additemview (St Ring Tabtext,fragment fragment,drawable drawable) {tabitemtextlist.add (Tabtext);p ageritemlist.add (Fragment); Tabitemdrawablelist.add (drawable); mfragmentpageradapter.notifydatasetchanged (); notifytabdatasetchanged ();} /** * Description: Delete one. * * @param index the index */public void RemoveiteMView (int index) {mtablayout.removeviewat (index);p ageritemlist.remove (index); tabitemlist.remove (index); Tabitemdrawablelist.remove (index); tabitemtextlist.remove (index); Mfragmentpageradapter.notifydatasetchanged (); notifytabdatasetchanged ();} /** * Description: Delete all. */public void Removeallitemviews () {mtablayout.removeallviews ();p ageritemlist.clear (); Tabitemlist.clear (); Tabitemdrawablelist.clear (); Tabitemtextlist.clear (); Mfragmentpageradapter.notifydatasetchanged (); notifytabdatasetchanged ();} /** * Description: Gets the viewpager of this view. * * @return The View pager */public Viewpager Getviewpager () {return mviewpager;} /** * Description: Sets the margin for each tab. * @param left, @param top the top * @param right, right * @param bottom the bottom */public void settabpadd ing (int left, int top, int. right, Int. bottom) {for (int i = 0;i<tabitemlist.size (); i++) {TextView TV = Tabitemlist.get (i) ; Tv.setpadding (left, top, right, bottom);}} }
Custom ViewPage
Resolve ViewPage Nested viewpage
Package Com.ferris.view;import Android.content.context;import Android.support.v4.view.viewpager;import Android.util.attributeset;import Android.view.view;public class Myviewpage extends Viewpager {public MyViewPage ( Context context) {super (context);//TODO auto-generated Constructor Stub}public myviewpage (context context, AttributeSet attrs) {Super (context, attrs);//TODO auto-generated constructor stub} @Overrideprotected Boolean CanScroll (View V, boolean checkv, int dx, int x, int y) {if (v! = This && v instanceof abslidingplayview) {//resolve ViewPage nested viewpage return true; } return Super.canscroll (V, checkv, DX, x, y);} If the child viewpager are at the end, scroll the parent////protected boolean canscroll (View V, boolean checkv, int dx, int x, int y) {//if (v! = This && v instanceof viewpager) {//int currentitem = ((Viewpager) v). Getcurr Entitem ();//int countitem = ((Viewpager) v). Getadapter (). GetCount ();/if (currentitem== (countitem-1) && dx<0) | | (currentitem==0 && dx>0)) {//return false;//}//return true;//}//return Super.canscroll (V, checkv, DX, x, y);//}}
[Application Girl--the third Main Interface Tab 2] Android app Market rapid development