本文实例为大家分享了Pagerslidingtabstrip菜单标题栏的具体代码,供大家参考,具体内容如下
1.加入library作为本地包工程,或者加入依赖在build.gradle(我对gradle不是很了解,所以用前面那个导入包的方法)
代码语言:javascript复制dependencies {
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
}
2、布局文件
代码语言:javascript复制<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
代码语言:javascript复制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();
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。