79
Step 3:隐藏顶部导航栏 同TextView的方式 Step 4:创建一个Fragment的简单布局与类 ,直接使用TextView中的~ Step 5: 主布局Activity的编写 package com.turing.base.activity.fragment.fragmentPractice2; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.widget.RadioButton; import android.widget.RadioGroup; import com.turing.base.R; /** * 我们使用LinearLayout + TextView实现了底部导航栏的效果,每次点击我们都要重置 所有TextView的状态, * 然后选中点击的TextView,有点麻烦是吧, * 接下来我们用另一种方法: RadioGroup + RadioButton来实现同样的效果 */ public class BottomNvgWithRadioButton extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{ private RadioGroup rg_tab_bar; private RadioButton rb_channel; //Fragment Object private Fragment_btm_nvg_rb_context fg1,fg2,fg3,fg4; private FragmentManager fManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bottom_nvg_with_radio_button); fManager = getSupportFragmentManager(); rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar); rg_tab_bar.setOnCheckedChangeListener(this); //获取第一个单选按钮,并设置其为选中状态 rb_channel = (RadioButton) findViewById(R.id.rb_channel); rb_channel.setChecked(true); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // FragmentTransaction只能使用一次, // 每次使用都要调用FragmentManager 的beginTransaction()方法获得FragmentTransaction事务对象 FragmentTransaction fTransaction = fManager.beginTransaction(); hideAllFragment(fTransaction); switch (checkedId){ case R.id.rb_channel: if(fg1 == null){ fg1 = new Fragment_btm_nvg_rb_context("第一个Fragment"); fTransaction.add(R.id.ly_content,fg1); }else{ fTransaction.show(fg1); } break; case R.id.rb_message: if(fg2 == null){ fg2 = new Fragment_btm_nvg_rb_context("第二个Fragment"); fTransaction.add(R.id.ly_content,fg2); }else{ fTransaction.show(fg2); } break; case R.id.rb_better: if(fg3 == null){ fg3 = new Fragment_btm_nvg_rb_context("第三个Fragment"); fTransaction.add(R.id.ly_content,fg3); }else{ fTransaction.show(fg3); } break; case R.id.rb_setting: if(fg4 == null){ fg4 = new Fragment_btm_nvg_rb_context("第四个Fragment"); fTransaction.add(R.id.ly_content,fg4); }else{ fTransaction.show(fg4); } break; } fTransaction.commit(); } //隐藏所有Fragment private void hideAllFragment(FragmentTransaction fragmentTransaction){ if(fg1 != null)fragmentTransaction.hide(fg1); if(fg2 != null)fragmentTransaction.hide(fg2); if(fg3 != null)fragmentTransaction.hide(fg3); if(fg4 != null)fragmentTransaction.hide(fg4); } }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
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
RadioGroup + RadioButton +ViewPager 效果图 分析我们在第二个实例的基础上(RadioButton方式) 加上ViewPager来实现滑动切换页面的效果。
ViewPager概念