Pagerslidingtabstrip菜单标题栏制作方法
程序员文章站
2023-11-26 16:31:34
本文实例为大家分享了pagerslidingtabstrip菜单标题栏的具体代码,供大家参考,具体内容如下
1.加入library作为本地包工程,或者加入依赖在bui...
本文实例为大家分享了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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。