本文实例讲述了android使用actionbar和viewpager切换页面,分享给大家供大家参考。具体如下:
运行效果截图如下:
项目布局如下:
具体代码如下:
mainactivity.java代码
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
import java.util.arraylist;
import java.util.list;
import android.os.bundle;
import android.support.v4.app.fragment;
import android.support.v4.app.fragmentmanager;
import android.support.v4.app.fragmentpageradapter;
import android.support.v4.app.fragmenttransaction;
import android.support.v4.view.viewpager;
import android.support.v4.view.viewpager.onpagechangelistener;
import android.support.v7.app.actionbar;
import android.support.v7.app.actionbar.tab;
import android.support.v7.app.actionbar.tablistener;
import android.support.v7.app.actionbaractivity;
import com.xuliugen.fragment.chatfragment;
import com.xuliugen.fragment.friendfragment;
import com.xuliugen.fragment.momentfragment;
import com.xuliugen.fragment.morefragment;
public class mainactivity extends actionbaractivity implements tablistener,onpagechangelistener {
private list<mytab> tabslist = new arraylist<mainactivity.mytab>( 4 );
private viewpager viewpager; // viewpager用于存放fragment
private actionbar actionbar;
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
// 添加4个fragment到自定义的tab中去
tabslist.add( new mytab( "聊天" , chatfragment. class ));
tabslist.add( new mytab( "朋友" , friendfragment. class ));
tabslist.add( new mytab( "时刻" , momentfragment. class ));
tabslist.add( new mytab( "更多" , morefragment. class ));
initactionbar();
}
private void initactionbar() {
// 找到viewpager
viewpager = (viewpager) this .findviewbyid(r.id.viewpager);
actionbar = getsupportactionbar(); // 获得v7中的actionbar
actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // 设置为导航模式
// 通过循环遍历将创建的四个tab加入到actionbar中
for (mytab tab : tabslist) {
tab t = actionbar.newtab(); // 使用v7下边的tab
t.settext(tab.gettext());
t.settablistener( this ); // 设置tablistener监听器
actionbar.addtab(t);
}
viewpager.setadapter( new tabfragmentpageradapter(getsupportfragmentmanager()));
viewpager.setonpagechangelistener( this );
}
/**
* 封装一个tab的类
*
* tab的文字
*
* tab所包含的class
*/
class mytab {
private string text; // 标题的文字
private class fragment; // 每一个tab所对应的页面fragment
public string gettext() {
return text;
}
public void settext(string text) {
this .text = text;
}
public class getfragment() {
return fragment;
}
public void setfragment( class fragment) {
this .fragment = fragment;
}
/**
* 两个参数的构造方法,便于创建一个对象
*
* @param string
* @param fragment
*/
public mytab(string string, class fragment) {
this .text = string;
this .fragment = fragment;
}
}
/**
* 为viewpager设置的适配器
*/
class tabfragmentpageradapter extends fragmentpageradapter {
public tabfragmentpageradapter(fragmentmanager fm) {
super (fm);
}
@override
public fragment getitem( int i) {
try {
return (fragment) tabslist.get(i).getfragment().newinstance();
} catch (instantiationexception e) {
e.printstacktrace();
} catch (illegalaccessexception e) {
e.printstacktrace();
}
return null ;
}
@override
public int getcount() {
return tabslist.size();
}
}
/************* 下边是tablistener所实现的方法 ***********************/
/**
* 当tab被双次选中
*/
@override
public void ontabreselected(tab tab, fragmenttransaction fragmenttransaction) {
}
/**
* 当tab被单次选中
*/
@override
public void ontabselected(tab tab, fragmenttransaction fragmenttransaction) {
viewpager.setcurrentitem(tab.getposition());
}
@override
public void ontabunselected(tab tab, fragmenttransaction fragmenttransaction) {
}
/**************** 下边是onpagechangelistener实现的方法 ****************/
@override
public void onpagescrollstatechanged( int i) {
}
@override
public void onpagescrolled( int i, float arg1, int arg2) {
}
@override
public void onpageselected( int i) {
actionbar.selecttab(actionbar.gettabat(i));
}
}
|
下边是四个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
|
public class chatfragment extends fragment {
@override
public view oncreateview(layoutinflater inflater, viewgroup container,
bundle savedinstancestate) {
return inflater.inflate(r.layout.fragment_friends, container, false );
}
}
public class friendfragment extends fragment {
@override
public view oncreateview(layoutinflater inflater, viewgroup container,
bundle savedinstancestate) {
return inflater.inflate(r.layout.fragment_chat, container, false );
}
}
public class momentfragment extends fragment {
@override
public view oncreateview(layoutinflater inflater, viewgroup container,
bundle savedinstancestate) {
return inflater.inflate(r.layout.fragment_moment, container, false );
}
}
public class morefragment extends fragment {
@override
public view oncreateview(layoutinflater inflater, viewgroup container,
bundle savedinstancestate) {
return inflater.inflate(r.layout.fragment_more, container, false );
}
}
|
activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<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"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= ".mainactivity" >
<android.support.v4.view.viewpager
android:id= "@+id/viewpager"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
</android.support.v4.view.viewpager>
</relativelayout>
|
下边是四个fragment的布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "fill_parent"
android:orientation= "vertical" >
<textview
android:id= "@+id/textview"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center_horizontal"
android:text= "聊天" >
</textview>
</linearlayout>
|
其他三个都一样只是文件名和里边的内容改变了一下。
希望本文所述对大家学习android软件编程有所帮助。