fragment实现多个页面切换


现在的APP首页大部分屏幕的下方显示一行Tab标签选项,点击不同的标签就可以切换到不同的界面。如下图:

Screenshot_2015-05-21-10-09-05

我们之前都是用TabHost来实现,但是殊不知,TabHost并非是那么的简单,它的可扩展性非常的差,不能随意地定制Tab项显示的内容,而且运行还要依赖于ActivityGroup。ActivityGroup原本主要是用于为每一个TabHost的子项管理一个单独的Activity,但目前已经被废弃了。下面就借助Fragment来完成类似于TabHost一般的效果。

先实现主界面布局main_layout.xml:

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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
 
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#000000" />
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#ffffff"
android:orientation="horizontal" >
 
<RelativeLayout
android:id="@+id/message_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
 
<ImageView
android:id="@+id/message_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="消息"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
 
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#000000" />
 
<RelativeLayout
android:id="@+id/contacts_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
 
<ImageView
android:id="@+id/contacts_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/contacts_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="联系人"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
 
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#000000" />
 
<RelativeLayout
android:id="@+id/news_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
 
<ImageView
android:id="@+id/news_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/news_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="动态"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
 
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#000000" />
 
<RelativeLayout
android:id="@+id/setting_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
 
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
 
<ImageView
android:id="@+id/setting_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/setting_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="设置"
android:textColor="#82858b" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
 
</LinearLayout>

这段布局代码虽然有点长,但其实主要就分为两部分。第一个部分就是FrameLayout,这里只是给FrameLayout的id设置成content,并没有在里面添加任何具体的内容,因为具体的内容是要在后面动态进行添加的。第二个部分就是FrameLayout下面的LinearLayout,这个LinearLayout中包含的就是整个类似于TabHost的布局。可以看到,我们将这个LinearLayout又等分成了四份,每一份中都会显示一个ImageView和一个TextView。ImageView用于显示当前Tab的图标,TextView用于显示当前Tab的标题。

既然是等分成了四分,那接下来我们自然要去分别实现四个Fragment和它们的布局了。新建一个message_layout.xml作为消息界面的布局,代码如下所示:

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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
 
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
 
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" />
 
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="这是消息界面"
android:textSize="20sp" />
</LinearLayout>
 
</RelativeLayout>

其他三个界面类似就不一 一列出。然后要去创建对应这个布局的Fragment。新建MessageFragment继承自Fragment,代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
public class MessageFragment extends Fragment{
    private TextView tv;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
        View messageLayout = inflater.inflate(R.layout.message, container, false);
        tv=(TextView) messageLayout.findViewById(R.id.message);
        tv.setText("哈哈哈哈哈哈");
        return messageLayout;
     }
 
}

我们也依次创建其他三个布局的Fragment。最后是MainActivity,代码如下:

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
public class MainActivity extends Activity implements OnClickListener {
/**
* 用于展示消息的Fragment
*/
private MessageFragment messageFragment;
 
/**
* 用于展示联系人的Fragment
*/
private ContactsFragment contactsFragment;
 
/**
* 用于展示动态的Fragment
*/
private NewsFragment newsFragment;
 
/**
* 用于展示设置的Fragment
*/
private SettingFragment settingFragment;
 
/**
* 消息界面布局
*/
private View messageLayout;
 
/**
* 联系人界面布局
*/
private View contactsLayout;
 
/**
* 动态界面布局
*/
private View newsLayout;
 
/**
* 设置界面布局
*/
private View settingLayout;
/**
* 用于对Fragment进行管理
*/
private FragmentManager fragmentManager;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      initViews();
      fragmentManager = getFragmentManager();
      // 第一次启动时选中第0个tab
      setTabSelection(0);
}
 
/**
* 在这里获取到每个需要用到的控件的实例,并给它们设置好必要的点击事件。
*/
 
private void initViews() {
      messageLayout = findViewById(R.id.message_layout);
      contactsLayout = findViewById(R.id.contacts_layout);
      newsLayout = findViewById(R.id.news_layout);
      settingLayout = findViewById(R.id.setting_layout);
      messageLayout.setOnClickListener(this);
      contactsLayout.setOnClickListener(this);
      newsLayout.setOnClickListener(this);
      settingLayout.setOnClickListener(this);
}
 
@Override
public void onClick(View v) {
     switch (v.getId()) {
     case R.id.message_layout:
         // 当点击了消息tab时,选中第1个tab
         setTabSelection(0);
         break;
      case R.id.contacts_layout:
          // 当点击了联系人tab时,选中第2个tab
          setTabSelection(1);
          break;
      case R.id.news_layout:
            // 当点击了动态tab时,选中第3个tab
            setTabSelection(2);
            break;
      case R.id.setting_layout:
             // 当点击了设置tab时,选中第4个tab
            setTabSelection(3);
             break;
      default:
        break;
}
 
}
 
/**
* 根据传入的index参数来设置选中的tab页。
*
* @param index
* 每个tab页对应的下标。0表示消息,1表示联系人,2表示动态,3表示设置。
*/
private void setTabSelection(int index) {
     // 每次选中之前先清楚掉上次的选中状态
     clearSelection();
     // 开启一个Fragment事务
     FragmentTransaction transaction = fragmentManager.beginTransaction();
     // 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
     hideFragments(transaction);
     switch (index) {
     case 0:
          messageLayout.setBackgroundColor(0xff0000ff);
 
          if (messageFragment == null) {
              // 如果MessageFragment为空,则创建一个并添加到界面上
               messageFragment = new MessageFragment();
              transaction.add(R.id.content, messageFragment);
          } else {
              // 如果MessageFragment不为空,则直接将它显示出来
              transaction.show(messageFragment);
          }
          break;
     case 1:
       // 当点击了联系人tab时,改变控件的图片和文字颜色
        contactsLayout.setBackgroundColor(0xff0000ff);
       if (contactsFragment == null) {
          // 如果ContactsFragment为空,则创建一个并添加到界面上
          contactsFragment = new ContactsFragment();
          transaction.add(R.id.content, contactsFragment);
       } else {
         // 如果ContactsFragment不为空,则直接将它显示出来
          transaction.show(contactsFragment);
        }
        break;
     case 2:
        // 当点击了动态tab时,改变控件的图片和文字颜色
         newsLayout.setBackgroundColor(0xff0000ff);
         if (newsFragment == null) {
            // 如果NewsFragment为空,则创建一个并添加到界面上
             newsFragment = new NewsFragment();
             transaction.add(R.id.content, newsFragment);
         } else {
           // 如果NewsFragment不为空,则直接将它显示出来
            transaction.show(newsFragment);
         }
         break;
      case 3:
          default:
          // 当点击了设置tab时,改变控件的图片和文字颜色
          settingLayout.setBackgroundColor(0xff0000ff);
          if (settingFragment == null) {
          // 如果SettingFragment为空,则创建一个并添加到界面上
          settingFragment = new SettingFragment();
          transaction.add(R.id.content, settingFragment);
          } else {
          // 如果SettingFragment不为空,则直接将它显示出来
          transaction.show(settingFragment);
         }
        break;
   }
  transaction.commit();
}
 
/**
* 将所有的Fragment都置为隐藏状态。
*
* @param transaction
* 用于对Fragment执行操作的事务
*/
private void hideFragments(FragmentTransaction transaction) {
      if (messageFragment != null) {
          transaction.hide(messageFragment);
      }
       if (contactsFragment != null) {
          transaction.hide(contactsFragment);
      }
       if (newsFragment != null) {
           transaction.hide(newsFragment);
      }
      if (settingFragment != null) {
         transaction.hide(settingFragment);
      }
}
 
/**
* 清除掉所有的选中状态。
*/
private void clearSelection() {
      messageLayout.setBackgroundColor(0xffffffff);
      contactsLayout.setBackgroundColor(0xffffffff);
      newsLayout.setBackgroundColor(0xffffffff);
      settingLayout.setBackgroundColor(0xffffffff);
      }
}

这样就实现了上面图中的效果了。

本文永久地址:http://blog.it985.com/11294.html
本文出自 IT985博客 ,转载时请注明出处及相应链接。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值