Android Tab and androidtab

Source: Internet
Author: User

Android Tab and androidtab

Original article address: Http://blog.csdn.net/crazy1235/article/details/42678877

Effect:Slide switch; click Label Switch.

 

Code:Https://github.com/ldb-github/Layout_Tab

1. Use ViewPager and PagerTabStrip/PagerTitleStrip for layout.

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "match_parent" android: layout_height = "match_parent"> <android. support. v4.view. viewPager android: id = "@ + id/javasth_vp" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_gravity = "center"> <android. support. v4.view. pagerTabStrip android: id = "@ + id/effecth_strip" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "# 7EC0EE" android: padding = "10dp"/> </android. support. v4.view. viewPager> </LinearLayout>Viewpager_pagertitle_pagertab.xml

 

1. To implement the getPageTitle () method of PagerAdapter, This method provides the tag name.

2. PaperTabStrip is a subclass of PagerTitleStrip. It adds the indicator function, that is, the small matrix under the label.

Public class extends Activity {// viewpager private ViewPager viewPager; // you only need to change PagerTabStrip in the Lyaout file to PagerTitleStrip private PagerTitleStrip; // viewpager indicator private PagerTabStrip tabStrip; // view set private List <View> viewList; // Title set private List <String> titleList; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. viewpager_pagertitle_pagertab); init ();} private void init () {viewList = new ArrayList <> (); LayoutInflater inflater = getLayoutInflater (); View = inflater. inflate (R. layout. viewpager_pageradapter_tab1, null); viewList. add (view); view = inflater. inflate (R. layout. viewpager_pageradapter_tab2, null); viewList. add (view); view = inflater. inflate (R. layout. viewpager_pageradapter_tab3, null); viewList. add (view); titleList = new ArrayList <> (); titleList. add ("Title 1"); titleList. add ("title 2"); titleList. add ("Title 3"); initViewPager ();} private void initViewPager () {viewPager = (ViewPager) findViewById (R. id. extends th_vp); viewPager. setAdapter (pagerAdapter); // modify the color of the indicator // tabStrip = (PagerTabStrip) findViewById (R. id. fifth_strip); // tabStrip. setTabIndicatorColor (Color. RED);}/**** adapter */PagerAdapter pagerAdapter = new PagerAdapter () {@ Override public int getCount () {return viewList. size () ;}@ Override public boolean isViewFromObject (View view, Object object) {return view = object ;}@ Override public Object instantiateItem (ViewGroup container, int position) {container. addView (viewList. get (position); return viewList. get (position) ;}@ Override public void destroyItem (ViewGroup container, int position, Object object) {container. removeView (viewList. get (position) ;}@ Override public CharSequence getPageTitle (int position) {return titleList. get (position );}};}ViewPagerAndPagerTitleOrPagerTabActivity. java

 

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.