Android-Tab single-choice control, android-tab single-choice control

Source: Internet
Author: User

Android-Tab single-choice control, android-tab single-choice control

Today, we can see that one of the controls in the project is very beautiful. It is said that it is an open-source control on github and the address is not found, as shown in. Very common results: switch back and forth between several tab pages:

Reprinted please indicate the source: http://blog.csdn.net/goldenfish1919/article/details/46799341

FlatTabGroup. java:

Public class FlatTabGroup extends RadioGroup implements RadioGroup. onCheckedChangeListener {public FlatTabGroup (Context context) {this (context, null);} private int mRadius; private int mStroke; private int mHighlightColor; private String [] mItemString; private float mTextSize; private ColorStateList mTextColor; private int [] mTabViewIds; private OnTabCheckedListener mTabCheckedListener; public F LatTabGroup (Context context, AttributeSet attrs) {super (context, attrs); setOrientation (HORIZONTAL); setGravity (Gravity. CENTER_VERTICAL); TypedArray array = context. obtainStyledAttributes (attrs, R. styleable. flatTabGroup); mHighlightColor = array. getColor (R. styleable. flatTabGroup_tab_border_color, Color. WHITE); mStroke = array. getDimensionPixelSize (R. styleable. flatTabGroup_tab_border_width, 2); mR Adius = array. getDimensionPixelOffset (R. styleable. flatTabGroup_tab_radius, 5); mTextColor = array. getColorStateList (R. styleable. flatTabGroup_tab_textColor); mTextSize = array. getDimensionPixelSize (R. styleable. flatTabGroup_tab_textSize, 14); int id = array. getResourceId (R. styleable. flatTabGroup_tab_items, 0); array. recycle (); mItemString = isInEditMode ()? New String [] {"tab a", "tab B", "TAB C"}: context. getResources (). getStringArray (id); generateTabView (context, attrs); super. setOnCheckedChangeListener (this);} private void generateTabView (Context context, AttributeSet attrs) {if (mItemString = null) {return;} mTabViewIds = new int [mItemString. length]; for (int I = 0; I <mItemString. length; I ++) {<span style = "white-space: pre"> </span> String tex T = mItemString [I]; RadioButton button = new RadioButton (context, attrs); button. setGravity (Gravity. CENTER); button. setButtonDrawable (android. r. color. transparent); button. setText (text); button. setTextColor (mTextColor); button. setTextSize (TypedValue. COMPLEX_UNIT_PX, mTextSize); button. setId (mTabViewIds [I] = generateViewIds (); LayoutParams lp = new LayoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. layoutParams. MATCH_PARENT, 1); if (I <mItemString. length-1) {<span style = "white-space: pre"> </span> lp. rightMargin =-1 * mStroke;} addView (button, lp) ;}} public void setOnTabCheckedListener (OnTabCheckedListener listener) {mTabCheckedListener = listener;} public void setSelection (int position) {check (getChildAt (position ). getId ();} @ Override public void onCheckedChanged (RadioGroup Group, int checkedId) {if (mTabCheckedListener! = Null) {int checkedPosition =-1; for (int I = 0; I <mTabViewIds. length; I ++) {if (mTabViewIds [I] = checkedId) {checkedPosition = I; break ;}} mTabCheckedListener. onChecked (this, checkedPosition) ;}@ Override protected void onFinishInflate () {super. onFinishInflate (); updateChildBackground () ;}@ SuppressWarnings ("deprecation") private void updateChildBackground () {for (int I = 0; I <getChildCount (); I ++) {View child = getChildAt (I); if (child instanceof RadioButton) {child. extends (generateTabBackground (I, mHighlightColor) ;}} private Drawable generateTabBackground (int position, int color) {// how to define selector StateListDrawable stateListDrawable = new StateListDrawable () using code (); stateListDrawable. addState (new int [] {android. r. attr. state_checked}, generateDrawable (position, color); stateListDrawable. addState (new int [] {}, generateDrawable (position, Color. TRANSPARENT); return stateListDrawable;} private Drawable generateDrawable (int position, int color) {float [] radius; if (position = 0) {radius = new float [] {mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius };} else if (position = getChildCount ()-1) {radius = new float [] {0, 0, mRadius, 0, 0 };} else {radius = new float [] {0, 0, 0, 0, 0, 0, 0, 0};} GradientDrawable shape = new GradientDrawable (); // how to generate a rounded shape using code. setCornerRadii (radius); shape. setColor (color); shape. setStroke (mStroke, mHighlightColor); return shape;} private static final AtomicInteger sNextGeneratedId = new AtomicInteger (1 ); // how to use custom id/*** Generate a value suitable for use in {@ link # setId (int )}. this value will * not collide with ID values generated at build time by aapt for R. id. ** @ return a generated ID value */public static int generateViewIds () {for (;) {final int result = sNextGeneratedId. get (); // aapt-generated IDs have the high byte nonzero; clamp to the range // under that. int newValue = result + 1; if (newValue> 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0. if (sNextGeneratedId. compareAndSet (result, newValue) {return result ;}} public static interface OnTabCheckedListener {public void onChecked (FlatTabGroup group, int position );}}
Here are a few interesting points:

(1) how to use code as selector

(2) how to use code to generate rounded corners

(3) how to use a custom id

Attr_flat_tab_group.xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="FlatTabGroup">        <attr name="tab_items" format="reference" />        <attr name="tab_border_width" format="dimension|reference" />        <attr name="tab_border_color" format="color|reference" />        <attr name="tab_radius" format="dimension|reference" />        <attr name="tab_textColor" format="reference" />        <attr name="tab_textSize" format="dimension|reference" />    </declare-styleable></resources>

Arrays_flat_tab_group.xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string-array name = "array_tabs_more_video"> <item> real-time live broadcast </item> <item> live broadcast reservation </item> <item> wonderful playback </item> </string-array> </resources>
When referencing:

<com.example.view.FlatTabGroup         android:id="@+id/flat_tab_group"         android:layout_width="match_parent"         android:layout_height="50dp"         android:paddingBottom="5dp"         android:paddingTop="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"         android:layout_gravity="center_vertical"         app:tab_border_color="#cdcdcd"         app:tab_border_width="1dp"         app:tab_items="@array/array_tabs_more_video"         app:tab_radius="5dp"         app:tab_textColor="@android:color/black"         app:tab_textSize="16sp"/>

      FlatTabGroup tabs = (FlatTabGroup)this.findViewById(R.id.flat_tab_group);      tabs.setOnTabCheckedListener(new OnTabCheckedListener(){@Overridepublic void onChecked(FlatTabGroup group, int position) {Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();}      });      tabs.setSelection(0);


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.