底部导航栏的几种实现方式 (2)

1)Fragment什么时候初始化和add到容器中? 
2)Fragment什么时候hide和show? 
3)如何让TextView被选中?选中一个TextView后,要做一些什么操作? 
4)刚进入MainActivity怎么样让一个TextView处于Selected的状态?

1)+2)我们选中TextView后对对应的Fragment进行判空,如果为空,初始化,并添加到容器中; 而hide的话,我们定义一个方法hide所有的Fragment,每次触发点击事件就先调用这个hideAll方法, 讲所有Fragment隐藏起来,然后如果TextView对应的Fragment不为空,我们就将这个Fragment显示出来;

3)这个我们通过点击事件来实现,点击TextView后先重置所有TextView的选中状态为false,然后设置点击的 TextView的选中状态为true; 
4)我们是通过点击事件来设置选中的,那么在onCreate()方法里加个触发点击事件的方法模拟点击就可以了~ txt_channel.performClick();

Code BottomNvgWithTextView.java package com.turing.base.activity.fragment.fragmentPractice1; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.FrameLayout; import android.widget.TextView; import com.turing.base.R; public class BottomNvgWithTextView extends AppCompatActivity implements View.OnClickListener { //UI Object private TextView txt_topbar; private TextView txt_channel; private TextView txt_message; private TextView txt_better; private TextView txt_setting; private FrameLayout ly_content; //Fragment Object private Fragment_btm_nvg_tv_context fg1, fg2, fg3, fg4; private FragmentManager fManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment__bottom_nvg_with_text_view); fManager = getSupportFragmentManager(); bindViews(); //模拟一次点击,既进去后选择第一项 txt_channel.performClick(); } /** * UI组件初始化与事件绑定 */ private void bindViews() { txt_topbar = (TextView) findViewById(R.id.txt_topbar); txt_channel = (TextView) findViewById(R.id.txt_channel); txt_message = (TextView) findViewById(R.id.txt_message); txt_better = (TextView) findViewById(R.id.txt_better); txt_setting = (TextView) findViewById(R.id.txt_setting); ly_content = (FrameLayout) findViewById(R.id.ly_content); txt_channel.setOnClickListener(this); txt_message.setOnClickListener(this); txt_better.setOnClickListener(this); txt_setting.setOnClickListener(this); } /** * 重置所有文本的选中状态 */ private void setSelected() { txt_channel.setSelected(false); txt_message.setSelected(false); txt_better.setSelected(false); txt_setting.setSelected(false); } /** * 隐藏所有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); } @Override public void onClick(View v) { FragmentTransaction fTransaction = fManager.beginTransaction(); hideAllFragment(fTransaction); switch (v.getId()) { case R.id.txt_channel: setSelected(); txt_channel.setSelected(true); if (fg1 == null) { fg1 = new Fragment_btm_nvg_tv_context("第一个Fragment"); fTransaction.add(R.id.ly_content, fg1); } else { fTransaction.show(fg1); } break; case R.id.txt_message: setSelected(); txt_message.setSelected(true); if (fg2 == null) { fg2 = new Fragment_btm_nvg_tv_context("第二个Fragment"); fTransaction.add(R.id.ly_content, fg2); } else { fTransaction.show(fg2); } break; case R.id.txt_better: setSelected(); txt_better.setSelected(true); if (fg3 == null) { fg3 = new Fragment_btm_nvg_tv_context("第三个Fragment"); fTransaction.add(R.id.ly_content, fg3); } else { fTransaction.show(fg3); } break; case R.id.txt_setting: setSelected(); txt_setting.setSelected(true); if (fg4 == null) { fg4 = new Fragment_btm_nvg_tv_context("第四个Fragment"); fTransaction.add(R.id.ly_content, fg4); } else { fTransaction.show(fg4); } break; } fTransaction.commit(); } }

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

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwxgdy.html