ViewPager的用法(3)——ViewPager嵌套Fragment

1、ViewPager嵌套Fragment的重点

(1)Activity继承了V4包下的FragmentActivity

(2)数据源中的List<View>   变成了List<Fragment>       
      初始化数据源的时候需要将数据使用fragment.setArguments(bundle)传入fragment中

(3)V4包下在Activity中获取FragmentManager时需要使用getSupportFragmentManager()方法

(4)自定义的PagerAdapter需要继承FragmentPagerAdapter

(5)在Fragment中需要在Activity加载完成的方法onActivityCreated(Bundle savedInstanceState)中 获取数据并更新UI

2、具体的步骤如下:

(1) 当前Activity继承  FragmentActivity

(2)在布局中写ViewPager和PagerTabStrip,并设置相关的属性

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

        <android.support.v4.view.PagerTabStrip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/pagerTabStrip"
            android:gravity="top">
        </android.support.v4.view.PagerTabStrip>
    </android.support.v4.view.ViewPager>
(3)初始化控件

				private ViewPager viewPager_vp;
				private PagerTabStrip pagerTab;
				。。。。
				。。。。
				viewPager_vp = (ViewPager) findViewById(R.id.viewPager_vp);
				pagerTab = (PagerTabStrip) findViewById(R.id.pagertab);

(4)创建Fragment,继承V4包下的fragment

xml文件fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    a
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值