Android custom rolling Tab and androidtab

Source: Internet
Author: User

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 );}
  • Underline
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 ();}}});}
  • Bind a listener
/*** 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!

Related Article

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.