[Application girl, application girl

Source: Internet
Author: User

[Application girl, application girl

In the app market, top game categories tab games and software categories are implemented using 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;/** tab background. */private int tabBackgroundResource =-1;/** the text of the tab is large. Small. */private int tabTextSize = 30;/** tab text color. */private int tabTextColor = Color. BLACK;/** the selected text color of the tab. */private int tabSelectColor = Color. BLACK;/** linear layout of the tab. */private LinearLayout mTabLayout = null;/** linear layout parent of the tab. */private HorizontalScrollView mTabScrollView = null;/** The m view pager. */private MyViewPage mViewPager;/** tab text. */private List <String> tabItemTextList = null;/** tab icon. */pri Vate List <Drawable> tabItemDrawableList = null;/View of ** content. */private ArrayList <Fragment> pagerItemList = null;/** tab list. */private ArrayList <TextView> tabItemList = null;/** content area adapter. */private AbFragmentPagerAdapter mFragmentPagerAdapter = null;/** The m tab click listener. */private OnClickListener mTabClickListener = new OnClickListener () {public void onClick (View view) {AbTabItemView tab View = (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, Helvetica, sans-serif;"> MyTab View </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 content of the View is adapted to 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 the Tab bar tabItemList = new ArrayList <TextView> (); tabItemTextList = new ArrayList <String> (); tabItemDrawableList = new ArrayList <Drawable> ();} public void initChilldManager (FragmentManager mFragmentManager) {// FragmentActi is required. Worker instance, if (mFragmentManager = null) {// if the above one is fragment, please pass it, childSupport to mFragmentManager = (FragmentActivity) this. context ). getsuppfrfragmentmanager ();} mFragmentPagerAdapter = new AbFragmentPagerAdapter (mFragmentManager, pagerItemList); mViewPager. setAdapter (mFragmentPagerAdapter); mViewPager. setOnPageChangeListener (new MyOnPageChangeListener (); mViewPager. setOffscreenPageLimit (3); this. addVie W (mViewPager, new LinearLayout. layoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT);}/*** The listener interface for inserting ing myOnPageChange events. * The class that is interested in processing a myOnPageChange * event implements this interface, and the object created * with that class is registered with a component using the * component's <code> addMyOnPageChangeListener <code> meth Od. when * the myOnPageChange event occurs, that object's appropriate * method is invoked. ** @ see MyOnPageChangeEvent */public class MyOnPageChangeListener implements OnPageChangeListener {/* (non-Javadoc) * @ see android. support. v4.view. viewPager. onPageChangeListener # onPageScrollStateChanged (int) */@ Overridepublic void onPageScrollStateChanged (int arg0) {if (mListener! = Null) {mListener. onPageScrollStateChanged (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. onPageScrolled (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 heightMeasureSpec) {final int widthMode = MeasureSpec. getMode (widthMeasureSpec); final boolean lockedExpanded = widthMode = MeasureSpec. EXACTLY; mTabScrollView. setFillViewport (lockedExpanded); final int childCount = mTab Layout. 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, heightMeasureSpe C); final int newWidth = getMeasuredWidth (); if (lockedExpanded & oldWidth! = NewWidth) {// Recenter the tab display if we're at a new (scrollable) size. setCurrentItem (mSelectedTabIndex);}/*** rolling animation ** @ param position the position */private void animateToTab (final int position) {final View tabView = mTabLayout. getChildAt (position); if (mTabSelector! = Null) {// remove removeCallbacks (mTabSelector) if the Selected tab is not null.} mTabSelector = new Runnable () {// public void run () {final int scrollPos = tabView. getLeft ()-(getWidth ()-tabView. getWidth ()/2; mTabScrollView. smoothScrollTo (scrollPos, 0); mTabSelector = null ;}}; post (mTabSelector) ;}/ * (non-Javadoc) * @ see android. view. view # onAttachedToWindow () */@ Override public void onAttachedTo Window () {super. onAttachedToWindow (); if (mTabSelector! = Null) {// Re-post the selector we saved post (mTabSelector);}/* (non-Javadoc) * @ see android. view. view # onDetachedFromWindow () */@ Override public void onDetachedFromWindow () {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) {addTab (text, index, null );} /*** Description: Create a Tab. ** @ param text the text * @ param index the index * @ param top the top */private void addTab (String text, int index, Drawable top) {AbTabItemView tabView = new AbTabItemView (this. context); if (t AbBackgroundResource! =-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: The tab changes and refreshes. */public void policytabdatasetchanged () {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, tabItemDrawableList. get (I);} else {addTab (tabItemTextList. get (I), I) ;}} if (mSelectedTabIndex> count) {mSelectedTabIndex = count-1 ;}setcurrentitem (mSelectedTabIndex); requestLayout () ;}/ *** description: set which one to display. ** @ param item the new current item */public void setCurrentItem (int item) {if (mViewPager = null) {throw new IllegalStateException ("ViewPager has 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: sets an external listener. ** @ param listener the new on page change listener */public void setOnPageChangeListener (ViewPager. onPageChangeListener listener) {mListener = listener;}/*** Description: Set the color of the tab text. ** @ param tabColor the new tab text color */public void setTabTextColor (int tabColor) {this. tabTextColor = tabColor;}/*** Description: Set the selected color. ** @ param tabColor the new tab select color */public void setTabSelectColor (int tabColor) {this. tabSelectColor = tabColor;}/*** Description: Set 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) {tabBackgroundResource = resid;}/*** Description: Set the Tab background. ** @ param resid the new tab layout background resource */public void setTabLayoutBackgroundResource (int resid) {this. mTabLayout. 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); pagerItemList. 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); pagerItemList. addAll (fragments); tabItemDrawableList. addAll (drawables); mFragmentPagerAdapter. notifyDataSetChanged (); notifyTabDataSetChanged ();}/*** Description: Add a content and tab. ** @ param tabText the tab text * @ param fragment the fragment */public void addItemView (String tabText, Fragment fragment) {tabItemTextList. add (tabText); pagerItemList. add (fragment); mFragmentPagerAdapter. notifyDataSetChanged (); notifyTabDataSetChanged ();}/*** Description: Add a content and tab. ** @ param tabText the tab text * @ param fragment the fragment * @ param drawable the drawable */public void addItemView (String tabText, Fragment fragment, Drawable drawable) {tabItemTextList. add (tabText); pagerItemList. add (fragment); tabItemDrawableList. add (drawable); mFragmentPagerAdapter. notifyDataSetChanged (); notifyTabDataSetChanged ();}/*** Description: delete a file. ** @ param index the index */public void removeItemView (int index) {mTabLayout. removeViewAt (index); pagerItemList. remove (index); tabItemList. remove (index); tabItemDrawableList. remove (index); tabItemTextList. remove (index); mFragmentPagerAdapter. notifyDataSetChanged (); notifyTabDataSetChanged ();}/*** Description: delete all. */public void removeAllItemViews () {mTabLayout. removeAllViews (); pagerItemList. clear (); tabItemList. clear (); tabItemDrawableList. clear (); tabItemTextList. clear (); mFragmentPagerAdapter. notifyDataSetChanged (); notifyTabDataSetChanged ();}/*** Description: obtains the ViewPager of the View. ** @ return the view pager */public ViewPager getViewPager () {return mViewPager;}/*** Description: sets the margins of each tab. ** @ param left the left * @ param top the top * @ param right the right * @ param bottom the bottom */public void setTabPadding (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
Solve 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) {// solves the nested viewpage return true;} return super. canScroll (v, checkV, dx, x, y);} // If the child viewpager is 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 ). getCurrentItem (); // 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 );//}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.