一聚教程网:一个值得你收藏的教程网站

热门教程

Pagerslidingtabstrip菜单标题栏制作方法

时间:2022-06-25 23:16:05 编辑:袖梨 来源:一聚教程网

本文实例为大家分享了Pagerslidingtabstrip菜单标题栏的具体代码,供大家参考,具体内容如下

1.加入library作为本地包工程,或者加入依赖在build.gradle(我对gradle不是很了解,所以用前面那个导入包的方法)

dependencies {
  compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
}

2、布局文件






3、MainActivity代码 有多少个标题就写多少个Fragment

public class MainActivity extends AppCompatActivity {

  private PagerSlidingTabStrip pst;
  private ViewPager vp;
  private List adlist;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pst = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    vp = (ViewPager) findViewById(R.id.vp);

    new Thread(){

      private String json;

      @Override
      public void run() {
        super.run();
        String path="http://www.meirixue.com/api.php?c=index&a=index";
        json = NetWork.getJson(path);

        runOnUiThread(new Runnable() {

          @Override
          public void run() {
            Gson gson = new Gson();
            Bean bean = gson.fromJson(json, Bean.class);
            adlist = bean.getData().getAdlist();
            vp.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
            pst.setViewPager(vp);
          }
        });

      }
    }.start();


  }
  //适配器
  class MyPagerAdapter extends FragmentPagerAdapter{


    public MyPagerAdapter(FragmentManager supportFragmentManager) {
      super(supportFragmentManager);
    }

    @Override
    public CharSequence getPageTitle(int position) {
      String name = adlist.get(position).getName();
      return name;
    }

    @Override
    public Fragment getItem(int position) {
      Fragment fragment=null;
      switch (position){
        case 0:
          fragment = new Fragment1();
          break;

        case 1:
          fragment = new Fragment2();
          break;

        case 2:
          fragment = new Fragment3();
          break;

        case 3:
          fragment = new Fragment4();
          break;
      }
      return fragment;
    }

    @Override
    public int getCount() {
      return adlist.size();
    }
  }
}


热门栏目