这篇文章将为大家详细讲解有关如何制作Pagerslidingtabstrip菜单标题栏,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
具体内容如下
加入library作为本地包工程,或者加入依赖在build.gradle(我对gradle不是很了解,所以用前面那个导入包的方法)
dependencies { compile 'com.astuetz:pagerslidingtabstrip:1.0.1'}
布局文件
<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>
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(); } }}
关于“如何制作Pagerslidingtabstrip菜单标题栏”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。