PagerSlidingTabStrip开源库实现ViewPager界面
2016-07-18 09:01
344 查看
PagerSlidingTabStrip的github地址
在布局文件中添加
将ViewPager和PgerSlidingTabStrip放在主界面中
然后新建一个布局文件pager_fragment.xml,将ViewPager的每一页设置为碎片。
再新建PagerFragment类继承Fragment,重写onCreateView()方法用来加载布局文件
在MainActivity中为ViewPager添加数据
在布局文件中添加
<android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1"/> <com.astuetz.PagerSlidingTabStrip android:id="@+id/tabstrip" android:layout_width="match_parent" android:layout_height="48dp" android:background="#f000" app:pstsShouldExpand="true" app:pstsIndicatorColor="#ff00cd79" app:pstsIndicatorHeight="4dp" />
将ViewPager和PgerSlidingTabStrip放在主界面中
然后新建一个布局文件pager_fragment.xml,将ViewPager的每一页设置为碎片。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.doubandemo.PagerFrament"> <TextView android:id="@+id/tv_frament" android:textSize="100dp" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout>
再新建PagerFragment类继承Fragment,重写onCreateView()方法用来加载布局文件
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //加载碎片布局 View view = inflater.inflate(R.layout.pager_frament,container,false); TextView tv = (TextView) view.findViewById(R.id.tv_frament); //取出MainActivity传递来的参数 Bundle bundle = getArguments(); //根据键取出对应的值 int num = bundle.getInt("fram_text"); //设置textview tv.setText(""+num); return view; }
在MainActivity中为ViewPager添加数据
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager())); //新建内部类继承FragmentPagerAdapter private class MyAdapter extends FragmentPagerAdapter { //重写构造方法 public MyAdapter(FragmentManager fm) { super(fm); } //加载position索引下的Fragment布局 @Override public Fragment getItem(int position) { Fragment fragment; //加载碎片,并将页码通过bundle传递 Bundle bundle = new Bundle(); fragment = new PagerFrament(); bundle.putInt("fram_text",position); fragment.setArguments(bundle); return fragment; } //获取滑动页面的个数 @Override public int getCount() { return titleList.size(); } //设置每个页面的标题 @Override public CharSequence getPageTitle(int position) { return titleList.get(position); } } //为pagerslidingtabStrip指定ViewPager pagerslidingtabStrip.setViewPager(viewPager);
相关文章推荐
- Java基础技巧
- 创建新节点
- Java连接PostgreSQL数据库
- 多个tomcat 共存需要修改的端口
- SwipeRefreshLayout实现下拉刷新
- php cookie工作原理与实例详解
- 8086汇编语言自学经验分享 e命令新旧内容同时看到
- 反思
- 移动开发注意几点
- HDU - 1021 Fibonacci Again
- C++11的新特性简单汇总介绍 (一)
- java/多线程
- "Xcode"意外退出
- HDU1258 Sum It Up
- 8086汇编语言自学经验分享 D命令查看指定范围内的内容
- 8086汇编语言自学经验分享 D命令查看指定地址的内容
- 【安卓面试题】Activity和Task的启动模式有哪些?每种含义是什么?举例说明各自的应用场景
- Gi之(二)基础命令
- python学习——电子邮件
- JS/CSS缓存杀手——VS插件