New android Tab control PagerSlidingTabStrip simplest use method

Source: Internet
Author: User

New android Tab control PagerSlidingTabStrip simplest use method

As we all know, the Tab control of Android is not very useful, so the PagerSlidingTabStrip project on Github is widely used. The project address is:

 

An example is shown below:

Because its demo code is long and hard to understand, I have summarized a simplest usage method.

First, import the library Project in PagerSlidingTabStrip In the ADT. If Android Studio is used, directly import the project. If ADT is used, you need to refer to my previous article for import:

Http://blog.csdn.net/logicteamleader/article/details/45202863

And renamed the library Project PagerSlidingTabStrip-lib.

Second, create your own project, which references the PagerSlidingTabStrip-lib project. Create several fragations in your project and inherit the FragmentPagerAdapter class to implement a MyPagerAdapter class. The Code is as follows (skip the layout and code of several fragations ):

 

Package wxbtabexam.apkkids.com; import java. util. arrayList; import android. OS. bundle; import android. support. v4.app. fragment; import android. support. v4.app. fragmentActivity; import android. support. v4.app. fragmentManager; import android. support. v4.app. fragmentPagerAdapter; import android. support. v4.view. viewPager; import android. util. typedValue; import com. astuetz. pagerSlidingTabStrip; import com. example. wxbtabexam. r; public class HomeActivity extends FragmentActivity {private ViewPager pager; private MyPagerAdapter pagerAdapter; private PagerSlidingTabStrip tabs; private int currentColor = 0xFF5161BC; @ Overrideprotected void onCreate (Bundle arg0. onCreate (arg0); setContentView (R. layout. home_layout); tabs = (PagerSlidingTabStrip) this. findViewById (R. id. tabs); pager = (ViewPager) this. findViewById (R. id. pager); ArrayList
 
  
Fragmentlist = new ArrayList
  
   
(); // Note that the three Fragment must implement ButtonFragment buttonFragment = new second (); TextFragment textFragment = new TextFragment (); MyListFragment mylistFragment = new second (); fragmentlist. add (buttonFragment); fragmentlist. add (textFragment); fragmentlist. add (mylistFragment); pagerAdapter = new MyPagerAdapter (getSupportFragmentManager (), fragmentlist); pager. setAdapter (pagerAdapter); final int pageMargin = (int) TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, 4, getResources (). getDisplayMetrics (); pager. setPageMargin (pageMargin); tabs. setViewPager (pager); changeColor (currentColor);} private void changeColor (int newColor) {tabs. setIndicatorColor (newColor); currentColor = newColor;} // The MyPagerAdapter corresponds to the preceding three fragations. class MyPagerAdapter extends FragmentPagerAdapter {private final String [] TITLES = {Buttons, TextViews, list}; private ArrayList
   
    
Fragmentlist; public MyPagerAdapter (FragmentManager fm, ArrayList
    
     
List) {super (fm); fragmentlist = list ;}@ Overridepublic CharSequence getPageTitle (int position) {return TITLES [position] ;}@ Overridepublic int getCount () {return fragmentlist. size ();} public Fragment getItem (int position) {return fragmentlist. get (position );}}}
    
   
  
 

In this case. The following is a runtime diagram. Although the Sample project provided by PagerSlidingTabStrip is not elegant, it has also achieved results. In addition, you can carefully study its sample project to obtain more methods.

 

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.