详解Android App中创建ViewPager组件的方法

时间:2022-12-03 00:21:44

现在很多app一打开就是一个viewpager,然后可以用手指滑,每滑一次就换一张图,底下还会有圈圈表示说现在滑到第几章~
通常这些图片都是放功能简介或是使用教学之类的,我的需求很简单,就是上面提到的那样而已。
有两种做法,一种是找现有套件,查了一堆资料每个都跟我推荐viewpagerindicator这套,我之前也看过这套,只是看起来需要有fragment再加上google play范例好像载不到了,所以只好自己实做一个。
viewpager的实作可参考android viewpager使用详解里面的程序码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@overrideprotected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_intro);
 
  mviewpager = (viewpager) findviewbyid(r.id.viewpager);
 
  final layoutinflater minflater = getlayoutinflater().from(this);
 
  view v1 = minflater.inflate(r.layout.intro_layout_1, null);
  view v2 = minflater.inflate(r.layout.intro_layout_2, null);
  view v3 = minflater.inflate(r.layout.intro_layout_3, null);
  view v4 = minflater.inflate(r.layout.intro_layout_4, null);
 
  viewlist = new arraylist<view>();
  viewlist.add(v1);
  viewlist.add(v2);
  viewlist.add(v3);
  viewlist.add(v4);
 
  mviewpager.setadapter(new myviewpageradapter(viewlist));
  mviewpager.setcurrentitem(0);}
myviewpageradapter
public class myviewpageradapter extends pageradapter {
  private list<view> mlistviews;
 
  public myviewpageradapter(list<view> mlistviews) {
    this.mlistviews = mlistviews;
  }
 
  @override
  public void destroyitem(viewgroup container, int position, object object)  {
    container.removeview((view) object);
  }
 
 
  @override
  public object instantiateitem(viewgroup container, int position) {
    view view = mlistviews.get(position);
    container.addview(view);
    return view;
  }
 
  @override
  public int getcount() {
    return mlistviews.size();
  }
 
  @override
  public boolean isviewfromobject(view arg0, object arg1) {
    return arg0==arg1;
  }}

这样子你就有一个viewpager了


下面我们来具体看一下viewpager的用法:
一、viewpager创建步骤
① 在xml布局中加入android.support.v4.view.viewpager

② 加载显示的页卡将layout布局转换为view对象

  (1) 

?
1
2
layoutinflater lf getlayoutinflater().from(this);
  lf.inflate(resource,root);

  (2)

?
1
view.inflate(context,resource,root);

③ 配置adapter(三种adapter)

 (1)pageradapter 数据源:list<view>
 (2)fragmentpageradapter 数据源:list<fragment>
 (3)fragmentstatepageradapter 数据源:list<fragment>
二、代码示例
新建四个fragment和他们的布局下面是mainactivity的xml布局:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<android.support.v4.view.viewpager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center" >
 
    <android.support.v4.view.pagertabstrip
      android:id="@+id/tab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="top" >
    </android.support.v4.view.pagertabstrip>
    <!-- 底部显示标题与上面的顶部显示不能同时出现
    <android.support.v4.view.pagertitlestrip
      android:id="@+id/title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom" >
    </android.support.v4.view.pagertitlestrip>
    -->
  </android.support.v4.view.viewpager>

第一种pageradapter

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class mypageradapter extends pageradapter {
 
  private list<view> viewlist;
  private list<string> titlelist;
 
  public mypageradapter(list<view> viewlist,list<string> titlelist){
    this.viewlist = viewlist;
    this.titlelist = titlelist;
  }
 
  /**
   * 返回页卡的数量
   */
  @override
  public int getcount() {
 
    return viewlist.size();
  }
 
   /**
   * view是否来自对象
   */
  @override
  public boolean isviewfromobject(view arg0, object arg1) {
 
    return arg0==arg1;
  }
   /**
   * 实例化一个页卡
   */
  @override
  public object instantiateitem(viewgroup container, int position) {
    container.addview(viewlist.get(position));
    return viewlist.get(position);
  }
   /**
   * 销毁一个页卡
   */
  @override
  public void destroyitem(viewgroup container, int position, object object) {
 
    container.removeview(viewlist.get(position));
 
  }
 
  /**
   * 设置viewpager的标题
   */
  @override
  public charsequence getpagetitle(int position) {
 
    return titlelist.get(position);
  }
 
}

第二种fragmentpageradapter

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class myfragmentpageradapter extends fragmentpageradapter {
  private list<fragment> fraglist;
  private list<string> titlelist;
  public myfragmentpageradapter(fragmentmanager fm,list<fragment> fraglist,list<string> titlelist) {
    super(fm);
    this.fraglist = fraglist;
    this.titlelist = titlelist;
  }
 
 
  @override
  public fragment getitem(int arg0) {
 
    return fraglist.get(arg0);
  }
 
  @override
  public charsequence getpagetitle(int position) {
 
    return titlelist.get(position);
  }
 
  @override
  public int getcount() {
 
    return fraglist.size();
  }
 
}

第三种fragmentstatepageradapter(该适配器可以动态销毁fragment)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class myfragmentpageradapter2 extends fragmentstatepageradapter {//该适配器可以动态销毁
  private list<fragment> fraglist;
  private list<string> titlelist;
  public myfragmentpageradapter2(fragmentmanager fm,list<fragment> fraglist,list<string> titlelist) {
    super(fm);
    this.fraglist = fraglist;
    this.titlelist = titlelist;
  }
 
 
  @override
  public fragment getitem(int arg0) {
 
    return fraglist.get(arg0);
  }
 
  @override
  public charsequence getpagetitle(int position) {
 
    return titlelist.get(position);
  }
 
  @override
  public int getcount() {
 
    return fraglist.size();
  }
 
  @override
  public object instantiateitem(viewgroup arg0, int arg1) {
 
    return super.instantiateitem(arg0, arg1);
  }
  @override
  public void destroyitem(viewgroup container, int position, object object) {
 
    super.destroyitem(container, position, object);
  }
 
}

mainactivity里使用getsupportfragmentmanager()该activity必须继承fragmentactivity:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class mainactivity extends fragmentactivity implements onpagechangelistener{
  private list<view> viewlist;
  private list<string> titlelist;
  private viewpager pager;
  private pagertabstrip tab;//顶部标题
 
  private list<fragment> fraglist;
 
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    viewlist = new arraylist<view>();
    titlelist = new arraylist<string>();
    tab = (pagertabstrip) findviewbyid(r.id.tab);
 
    view view1 = view.inflate(this,r.layout.view1,null);
    view view2 = view.inflate(this,r.layout.view2,null);
    view view3 = view.inflate(this,r.layout.view3,null);
    view view4 = view.inflate(this,r.layout.view4,null);
 
    viewlist.add(view1);
    viewlist.add(view2);
    viewlist.add(view3);
    viewlist.add(view4);
 
    fraglist = new arraylist<fragment>();
    fraglist.add(new fragment1());
    fraglist.add(new fragment2());
    fraglist.add(new fragment3());
    fraglist.add(new fragment4());
 
    //为viewpager页卡设置标题
    titlelist.add("第一页");
    titlelist.add("第二页");
    titlelist.add("第三页");
    titlelist.add("第四页");
    //为pagertabstrip设置一些属性
    tab.setbackgroundcolor(color.white);
    tab.setdrawfullunderline(false);
    tab.settabindicatorcolor(color.blue);
 
    pager = (viewpager) findviewbyid(r.id.pager);
    //mypageradapter adapter = new mypageradapter(viewlist,titlelist);
    //myfragmentpageradapter adapter = new myfragmentpageradapter(getsupportfragmentmanager(), fraglist, titlelist);
    /**
     * 使用getsupportfragmentmanager()该activity必须继承fragmentactivity
     */
    myfragmentpageradapter2 adapter = new myfragmentpageradapter2(getsupportfragmentmanager(), fraglist, titlelist);
    pager.setadapter(adapter);
    pager.setonpagechangelistener(this);
  }
 
  @override
  public void onpagescrollstatechanged(int arg0) {
 
  }
 
  @override
  public void onpagescrolled(int arg0, float arg1, int arg2) {
 
  }
 
  @override
  public void onpageselected(int arg0) {
    toast.maketext(this,"当前是第"+(arg0+1)+"个界面", 0).show();
 
  }
 
}