标签Tab切换 PagerSlidingTabStrip的使用

一、依赖库

compile 'cc.tracyzhang:PagerSlidingTabStrip:1.1.1'

二、布局




            //滑动条所在底部长线的高度

    

三、代码

public class CollectActivity extends AppCompatActivity {
private Fragment[] mFragemnts = new Fragment[]{new SWFragment(),  new MessageFragment(), new BlogsFragment(),new TopicFragment(), new CodeFragment()};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_collect);

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);

    ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
    viewpager.setAdapter(new MyAdapter(getSupportFragmentManager()));

    tabs.setTextSize(18);
    tabs.setTabBackground(Color.TRANSPARENT);
    tabs.setTextColorStateListResource(R.drawable.tab_text_color_selector);//标签字体颜色状态
    tabs.setBackgroundColor(Color.WHITE);
    tabs.setViewPager(viewpager);
}

public class MyAdapter extends FragmentPagerAdapter {
    private String[] titles = new String[]{"Tab1", "Tab2", "Tab3", "Tab4", "Tab5"};

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragemnts[position];
    }

    @Override
    public int getCount() {
        return mFragemnts.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

}

你可能感兴趣的:(第三方)