亲宝软件园·资讯

展开

Pagerslidingtabstrip菜单标题栏 Pagerslidingtabstrip菜单标题栏制作方法

jie哈哈哈 人气:0
想了解Pagerslidingtabstrip菜单标题栏制作方法的相关内容吗,jie哈哈哈在本文为您仔细讲解Pagerslidingtabstrip菜单标题栏的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Pagerslidingtabstrip,菜单,标题栏,下面大家一起来学习吧。


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

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

2、布局文件

<com.astuetz.PagerSlidingTabStrip
  android:id="@+id/tabs"
  android:layout_width="match_parent"
  android:layout_height="48dip" />

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

</android.support.v4.view.ViewPager>

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

public class MainActivity extends AppCompatActivity {

  private PagerSlidingTabStrip pst;
  private ViewPager vp;
  private List<Bean.DataBean.AdlistBean> 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();
    }
  }
}


加载全部内容

相关教程
猜你喜欢
用户评论