ViewPager详解

ViewPager用于实现多页面的切换效果,该类存在于Google的兼容包里面,所以在引用时记得在BuilldPath中加入“android-support-v4.jar”。

support v4是google官方给我们提供的一个兼容低版本安卓设备的软件包,里面包含了只有在安卓3.0以上可以使用的api。

使用ViewPager与ListView类似,也需要一个适配器,PagerAdapter。

ViewPager的功能就是可以使视图滑动,就像Lanucher左右滑动那样。分三个步骤来使用它:

1、布局文件
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >

<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" >
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>

注意:
1、要写全路径名。
2、ViewPager是滑动的内容部分,里面包含PageTitleStrip是上面的指示文字标题。(相当于Tab标签页部分)
可以使用PageTabStrip(PageTitleStrip子类),有交互的功能。(点击标题也可以实现滑动功能)。看一些API说明:

PagerTabStrip is an interactive indicator of the current, next, and previous pages of a ViewPager. It is intended to be used as a child view of a ViewPager widget in your XML layout. Add it as a child of a ViewPager in your layout file and set its android:layout_gravity to TOP or BOTTOM to pin it to the top or bottom of the ViewPager. The title from each page is supplied by the method getPageTitle(int) in the adapter supplied to the ViewPager.

For a non-interactive indicator, see PagerTitleStrip.


2、加需所要显示的页卡,标题
private List<View> views;
private List<String> titles;

LayoutInflater layoutInflater = getLayoutInflater();
View view1 = layoutInflater.inflate(R.layout.view1, null);
View view2 = layoutInflater.inflate(R.layout.view2, null);
View view3 = layoutInflater.inflate(R.layout.view3, null);

views.add(view1);
views.add(view2);
views.add(view3);

titles = new ArrayList<String>();
titles.add("tab1");
titles.add("tab2");
titles.add("tab3");

3、实例化PageAdapter, 并加载adapter

PageAdapter:
Base class providing the adapter to populate pages inside of a ViewPager. You will most likely want to use a more specific implementation of this, such as FragmentPagerAdapter or FragmentStatePagerAdapter.

When you implement a PagerAdapter, you must override the following methods at minimum:

instantiateItem(ViewGroup, int)
destroyItem(ViewGroup, int, Object)
getCount()
isViewFromObject(View, Object)

PagerAdapter pageAdapter = new PagerAdapter() {


@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager)container).removeView(views.get(position));
}

@Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
((ViewPager)container).addView(views.get(position));
return views.get(position);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return views.size();
}

@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titles.get(position);
}
};

viewPager.setAdapter(pageAdapter);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值