Android custom rolling Tab and androidtab
Requirements
Tab labels can be horizontally rolled, selected, and underlined.
Analysis
It can inherit the HorizontalScrollView implementation, and the label ITem can be TextView, And the underline can be painted in the Draw method.
Implementation
- Add a Tab Item (TextView)
/*** Add the label view to the container ** @ param position * @ param title */private void add *** (final int position, String title) {TextView tab = new TextView (getContext (); tab. setText (title); tab. setGravity (Gravity. CENTER); tab. setSingleLine (); if (position = currentPosition) {tab. setTextColor (mTabPressTextColor);} else {tab. setTextColor (mTabTextColor);} tab. setTextSize (TypedValue. COMPLEX_UNIT_PX, mTabTextSize); tab. setBackgroundResource (tabBackgroundResId); addTab (position, tab );}
Protected void onDraw (Canvas canvas) {super. onDraw (canvas);/** draw the tab underline **/View currentTab = tabsContainer. getChildAt (currentPosition); if (currentTab! = Null) {float lineLeft = currentTab. getLeft (); canvas. drawRect (lineLeft-detalLeft, mViewHeight-underlineHeight, lineLeft + currentTab. getWidth ()-detalRight, mViewHeight, mLinePaint )'}}
- Implement underline animation for Item selection
/*** Simulate animated rolling underline ** @ param fromPosition * @ param toPosition */private void ***** (int fromPosition, int toPosition) {TextView lastTab = (TextView) tabsContainer. getChildAt (fromPosition); lastTab. setTextColor (mTabTextColor); TextView currentTab = (TextView) tabsContainer. getChildAt (toPosition); currentTab. setTextColor (mTabPressTextColor); currentPosition = toPosition; float lineLeft = currentTab. getLeft (); float lineRight = currentTab. getRight (); detalLeft = lineLeft-lastTab. getLeft (); detalRight = lineRight-lastTab. getRight (); this. post (new Runnable () {@ Override public void run () {if (Math. abs (detalLeft)> minDetal | Math. abs (detalRight)> minDetal) {if (Math. abs (detalLeft)> minDetal) {detalLeft = detalLeft/minDetal;} if (Math. abs (detalRight)> minDetal) {detalRight = detalRight/minDetal;} invalidate (); TabHorizontalScrollView. this. post (this) ;}else {invalidate ();}}});}
/*** Tag listening event */private OnTabItemClickListener mOnTabItemClickListener;/*** bind tag switching listener event ** @ param listener */public void setOnTabItemClickListener (OnTabItemClickListener) {mOnTabItemClickListener = listener;}/*** tag listener class ** @ author jarlen **/public interface OnTabItemClickListener {public void onClickTabItem (float value );} call the onClickTabItem () method in the OnClick method of the TextView of Item, and then Y.
If you want this Demo,
Code = money.
Please click on a temporary QQ chat. If You Are the One!