ViewPager是android擴展包v4包中的類,這個類能夠讓用戶左右切換當前的view。咱們首先來看看API對於這個類的表述:html
1
2
3
|
Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.
Note this class is currently under early design and development. The API will likely change in later updates of the compatibility library, requiring changes to the source code of apps when they are compiled against the newer version.
ViewPager is most often used in conjunction with Fragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These are FragmentPagerAdapter andFragmentStatePagerAdapter; each of these classes have simple code showing how to build a full user interface with them.
|
從這個描述中咱們知道幾點:java
1)ViewPager類直接繼承了ViewGroup類,全部它是一個容器類,能夠在其中添加其餘的view類。android
2)ViewPager類須要一個PagerAdapter適配器類給它提供數據。app
3)ViewPager常常和Fragment一塊兒使用,而且提供了專門的FragmentPagerAdapter和FragmentStatePagerAdapter類供Fragment中的ViewPager使用。ide
在編寫ViewPager的應用的使用,還須要使用兩個組件類分別是PagerTitleStrip類和PagerTabStrip類,PagerTitleStrip類直接繼承自ViewGroup類,而PagerTabStrip類繼承PagerTitleStrip類,因此這兩個類也是容器類。可是有一點須要注意,在定義XML的layout的時候,這兩個類必須是ViewPager標籤的子標籤,否則會出錯。ui
layout.xml:this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
android.support.v4.view.ViewPager
android:id
=
"@+id/viewpager"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
>
<
android.support.v4.view.PagerTabStrip
android:id
=
"@+id/tabstrip"
android:layout_width
=
"wrap_content"
android:layout_height
=
"50dip"
android:gravity
=
"center"
/>
</
android.support.v4.view.ViewPager
>
</
RelativeLayout
>
|
MainActivity.javaspa
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
package
com.example.android_viewpager1;
import
java.util.ArrayList;
import
android.annotation.SuppressLint;
import
android.app.Activity;
import
android.os.Bundle;
import
android.support.v4.view.PagerAdapter;
import
android.support.v4.view.PagerTabStrip;
import
android.support.v4.view.ViewPager;
import
android.support.v4.view.ViewPager.OnPageChangeListener;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
public
class
MainActivity
extends
Activity {
ViewPager pager =
null
;
PagerTabStrip tabStrip =
null
;
ArrayList<View> viewContainter =
new
ArrayList<View>();
ArrayList<String> titleContainer =
new
ArrayList<String>();
public
String TAG =
"tag"
;
@SuppressLint
(
"ResourceAsColor"
)
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager)
this
.findViewById(R.id.viewpager);
tabStrip = (PagerTabStrip)
this
.findViewById(R.id.tabstrip);
//取消tab下面的長橫線
tabStrip.setDrawFullUnderline(
false
);
//設置tab的背景色
tabStrip.setBackgroundColor(
this
.getResources().getColor(R.color.bg));
//設置當前tab頁籤的下劃線顏色
tabStrip.setTabIndicatorColor(
this
.getResources().getColor(R.color.red));
tabStrip.setTextSpacing(
200
);
View view1 = LayoutInflater.from(
this
).inflate(R.layout.tab1,
null
);
View view2 = LayoutInflater.from(
this
).inflate(R.layout.tab2,
null
);
View view3 = LayoutInflater.from(
this
).inflate(R.layout.tab3,
null
);
View view4 = LayoutInflater.from(
this
).inflate(R.layout.tab4,
null
);
//viewpager開始添加view
viewContainter.add(view1);
viewContainter.add(view2);
viewContainter.add(view3);
viewContainter.add(view4);
//頁籤項
titleContainer.add(
"網易新聞"
);
titleContainer.add(
"網易體育"
);
titleContainer.add(
"網易財經"
);
titleContainer.add(
"網易女人"
);
pager.setAdapter(
new
PagerAdapter() {
//viewpager中的組件數量
@Override
public
int
getCount() {
return
viewContainter.size();
}
//滑動切換的時候銷燬當前的組件
@Override
public
void
destroyItem(ViewGroup container,
int
position,
Object object) {
((ViewPager) container).removeView(viewContainter.get(position));
}
//每次滑動的時候生成的組件
@Override
public
Object instantiateItem(ViewGroup container,
int
position) {
((ViewPager) container).addView(viewContainter.get(position));
return
viewContainter.get(position);
}
@Override
public
boolean
isViewFromObject(View arg0, Object arg1) {
return
arg0 == arg1;
}
@Override
public
int
getItemPosition(Object object) {
return
super
.getItemPosition(object);
}
@Override
public
CharSequence getPageTitle(
int
position) {
return
titleContainer.get(position);
}
});
pager.setOnPageChangeListener(
new
OnPageChangeListener() {
@Override
public
void
onPageScrollStateChanged(
int
arg0) {
Log.d(TAG,
"--------changed:"
+ arg0);
}
@Override
public
void
onPageScrolled(
int
arg0,
float
arg1,
int
arg2) {
Log.d(TAG,
"-------scrolled arg0:"
+ arg0);
Log.d(TAG,
"-------scrolled arg1:"
+ arg1);
Log.d(TAG,
"-------scrolled arg2:"
+ arg2);
}
@Override
public
void
onPageSelected(
int
arg0) {
Log.d(TAG,
"------selected:"
+ arg0);
}
});
}
}
|
實現的效果以下:.net
對於PagerAdapter類,android的文檔已經說的很清楚了,必須至少實現以下的4個方法,若是須要更好的擴展也能夠實現更多的方法。code
public Object instantiateItem(ViewGroup container, int position)
public void destroyItem(ViewGroup container, int position,Object object)
public int getCount()
public boolean isViewFromObject(View arg0, Object arg1