上次写过App界面Tab选项卡之ViewPager,今天就谈谈另一种实现方式,采用Fragment的方式,关于Fragment有篇非常好的文章,不太熟悉的先自行前往get点我点我。
今天只是Fragment初探,目的还是实现ViewPager一样的界面,但是由于Fragment自身并不支持左右滑动,须自行实现,所以这次只能靠点击下面的bottom_bar进行页面切换。在后面我会写一个综合版的,让Fragment也可以滑起来。
title_bar和bottom_bar都是采用的ViewPager那一篇中的,唯一值得注意的主界面,activity_main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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='match_parent'
android:orientation='vertical'>
<include layout='@layout/title_bar' />
<FrameLayout
android:id='@+id/frame_layout'
android:layout_width='fill_parent'
android:layout_height='0dp'
android:layout_weight='1' />
<include layout='@layout/bottom_bar' />
</LinearLayout>
本该显示切换页面的位置由ViewPager换成的FrameLayout,上篇中内容布局不是以inflate动态加载出来的么,然后放在views中,这次就不这样做了,新建4个类,都继承自Fragment,因为大同小异,我就贴一个代码了,Fragment_0.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.cjt_pc.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by cjt-pc on 2015/8/21.
* Email:879309896@qq.com
*/
public class Fragment_0 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_0, null);
return view;
}
}
后面就相应的改类的名称和加载的布局id就可以了,最重要的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
package com.cjt_pc.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private Fragment fragment_0, fragment_1, fragment_2, fragment_3;
// ViewPager指示器布局
private LinearLayout llIndicator0, llIndicator1, llIndicator2, llIndicator3;
private ImageView ivImg0, ivImg1, ivImg2, ivImg3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWidget();
setSelector(0);
}
private void initWidget() {
llIndicator0 = (LinearLayout) findViewById(R.id.tab_0);
llIndicator0.setOnClickListener(this);
llIndicator1 = (LinearLayout) findViewById(R.id.tab_1);
llIndicator1.setOnClickListener(this);
llIndicator2 = (LinearLayout) findViewById(R.id.tab_2);
llIndicator2.setOnClickListener(this);
llIndicator3 = (LinearLayout) findViewById(R.id.tab_3);
llIndicator3.setOnClickListener(this);
ivImg0 = (ImageView) findViewById(R.id.img_0);
ivImg1 = (ImageView) findViewById(R.id.img_1);
ivImg2 = (ImageView) findViewById(R.id.img_2);
ivImg3 = (ImageView) findViewById(R.id.img_3);
}
@Override
public void onClick(View view) {
resetIndicator();
switch (view.getId()) {
case R.id.tab_0:
setSelector(0);
break;
case R.id.tab_1:
setSelector(1);
break;
case R.id.tab_2:
setSelector(2);
break;
case R.id.tab_3:
setSelector(3);
break;
}
}
public void setSelector(int selector) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
hideFragment(transaction);
switch (selector) {
case 0:
if (fragment_0 == null) {
fragment_0 = new Fragment_0();
transaction.add(R.id.frame_layout, fragment_0);
} else {
transaction.show(fragment_0);
}
ivImg0.setImageResource(R.mipmap.tab_weixin_pressed);
break;
case 1:
if (fragment_1 == null) {
fragment_1 = new Fragment_1();
transaction.add(R.id.frame_layout, fragment_1);
} else {
transaction.show(fragment_1);
}
ivImg1.setImageResource(R.mipmap.tab_find_frd_pressed);
break;
case 2:
if (fragment_2 == null) {
fragment_2 = new Fragment_2();
transaction.add(R.id.frame_layout, fragment_2);
} else {
transaction.show(fragment_2);
}
ivImg2.setImageResource(R.mipmap.tab_address_pressed);
break;
case 3:
if (fragment_3 == null) {
fragment_3 = new Fragment_3();
transaction.add(R.id.frame_layout, fragment_3);
} else {
transaction.show(fragment_3);
}
ivImg3.setImageResource(R.mipmap.tab_settings_pressed);
break;
}
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction) {
if (fragment_0 != null) {
transaction.hide(fragment_0);
}
if (fragment_1 != null) {
transaction.hide(fragment_1);
}
if (fragment_2 != null) {
transaction.hide(fragment_2);
}
if (fragment_3 != null) {
transaction.hide(fragment_3);
}
}
private void resetIndicator() {
ivImg0.setImageResource(R.mipmap.tab_weixin_normal);
ivImg1.setImageResource(R.mipmap.tab_find_frd_normal);
ivImg2.setImageResource(R.mipmap.tab_address_normal);
ivImg3.setImageResource(R.mipmap.tab_settings_normal);
}
}
我要指出的就是在四个fragment并没有在onCreate中全部初始化,而是放在setSelector中,这样做的好处的是只有当使用到的时候通过if判断,若fragment为null才new一个出来,具体的用法还是参照最上面详解fragment链接。
相关代码下载:http://download.csdn.net/detail/qq_15002323/9038173
猿圈 (ID: CodePush )(←长按复制)
为程序员提供最优质的博文、最精彩的讨论、最实用的开发资源;提供最新最全的编程学习资料:PHP、Objective-C、Java、Swift、C/C++函数库、.NET Framework类库、J2SE API等等。并不定期奉送各种福利。