首页 > 代码库 > PagerSlidingTabStrip

PagerSlidingTabStrip

PagerSlidingTabStrip

介绍:用于viewPager和上面的标签Tab 关联,功能丰富

     1,studio网络库依赖包

      2,把Fragment 或者View 提供title文字,放进ViewPager的Adapter的四个方法中

      3,viewPager.setAdapter( new MyAdapter())

      4, Tab.setViewPager();

布局文件中,Tab和ViewPager是上下关系

<com.astuetz.PagerSlidingTabStrip  />

<ViewPager />

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        
        app:pstsIndicatorColor="#0f0"
        app:pstsIndicatorHeight="2dp"
        app:pstsIndicatorMarginLeftRight="10dp"
        //标签下划线的颜色和高度
        app:pstsUnderlineColor="#f00"
        app:pstsUnderlineHeight="1dp"

        app:pstsShouldExpand="true"

        app:pstsDividerColor="#00f"
        app:pstsDividerWidth="1dp"
        app:pstsDividerPadding="5dp"

        app:pstsTabTextStyle="bold|italic"
        app:pstsTabTextColor="@color/tab_selector"
        app:pstsTabBackground="@drawable/tab_bg"      标签被选中时候的背景色
        app:pstsTabTextSize="30sp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>
</LinearLayout>

在Fragment 中

package cn.itcast.demo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;

import junit.framework.Test;

import java.util.ArrayList;
import java.util.List;


public class NewsFragment extends Fragment {

    @butterknife.InjectView(R.id.tablayout)
    PagerSlidingTabStrip tablayout;
    @butterknife.InjectView(R.id.viewpager)
    ViewPager viewpager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, null);

        butterknife.ButterKnife.inject(this, view);

        List<Fragment> fragmentlist = new ArrayList<>();
        fragmentlist.add(TestFragment.newInstance("这是 资讯 界面"));
        fragmentlist.add(TestFragment.newInstance("这是 热点 界面"));
        fragmentlist.add(TestFragment.newInstance("这是 博客 界面"));
        fragmentlist.add(TestFragment.newInstance("这是 推荐 界面"));

        viewpager.setAdapter(new MyViewPagerAdapter(getFragmentManager(),fragmentlist));

        // 关联标题栏和Viewpager
        tablayout.setViewPager(viewpager);
//        tablayout.setTextColorStateListResource(R.color.tab_selector);
设置标签文字被选中的颜色
System.out.println("开启任务,"+Thread.currentThread()); asyncTask.execute("你好","好不好","还好"); return view; } @Override public void onDestroyView() { super.onDestroyView(); butterknife.ButterKnife.reset(this); } }
//设置ViewPager的 Adapter
public
class MyViewPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragmentList; public MyViewPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) { super(fm); this.fragmentList = fragmentList; } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } @Override public CharSequence getPageTitle(int position) { return "界面 "+position; }

 

PagerSlidingTabStrip