fragmenttabhost怎么替换fragment的内容
1个回答
2015-10-30
展开全部
一直以来大概做带有导航类的应用的时候,都是使用TabActivity,但是,最新的SDK把TabActivity标成了deprecated,那只能重新寻找替代方案了,目前网上说的方案都是很混乱的
Fragment翻译过来叫碎片,但是我个人觉得可以理解为具有生命周期的View
由于Fragment是3.0以后的东西,对于适应低版本,那我们只能使用谷歌官方提供的android-support-v4.jar这个包了,最快捷的办法就是右击工程名,选择Android Tools——add support library...,即可添加这个包了
由于3.0以下的Activity里面没有对Fragment的支持,所以选择继承android-support-v4.jar包中的FragmentActivity,其功能跟3.0及以后的版本的Activity的功能一样
DEMO下载:
FragmentTest.zip
底部导航加顶部导航,使用FragmentTabHost,Fragmetn,FragmentActivity组成,代码已经打包上传了
1、首先,底部导航的实现,FragmentTabHost和以前的TabHost的区别就是增加了对Fragment的支持,这里的实现和其他使用TabActivity是一样的,只是选项卡的切换已经从activity变成fragment了
2、这里主要讲一下顶部导航的实现,为了让大家对fragment更好的理解,这里我没有采用FragmentTabHost,而是放了三个Button
[java] view plaincopyprint?
01. package com.yyb.fragmenttest;
02.
03. import android.os.Bundle;
04. import android.support.v4.app.FragmentTabHost;
05. import android.support.v4.app.FragmentTransaction;
06. import android.view.LayoutInflater;
07. import android.view.View;
08. import android.view.ViewGroup;
09. import android.view.View.OnClickListener;
10.
11. public class Fragment1 extends BaseFragment implements OnClickListener{
12.
13. public static BaseFragment newInstance(int index) {
14. BaseFragment fragment = new Fragment1();
15. Bundle args = new Bundle();
16. args.putInt("index", index);
17. fragment.setArguments(args);
18. fragment.setIndex(index);
19. return fragment;
20. }
21.
22. private View layoutView;
23. private FragmentTabHost mTabHost;
24.
25. @Override
26. public void onCreate(Bundle savedInstanceState) {
27. super.onCreate(savedInstanceState);
28. }
29.
30. @Override
31. public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
32.
33. layoutView = inflater.inflate(R.layout.fragment_tabs, null);
34.
35. layoutView.findViewById(R.id.button1).setOnClickListener(this);
36. layoutView.findViewById(R.id.button1).setSelected(true);
37. layoutView.findViewById(R.id.button2).setOnClickListener(this);
38. layoutView.findViewById(R.id.button3).setOnClickListener(this);
39.
40. if(savedInstanceState ==null){
41. BaseFragment fragment = (BaseFragment)getChildFragmentManager().findFragmentByTag(0+"");// getActivity().getSupportFragmentManager().findFragmentByTag(index+"");
42. if(fragment==null){
43. init(0);
44. }
45. }
46.
47. return layoutView;
48. }
49.
50. private void init(int index) {
51. FragmentTransaction ft = getChildFragmentManager().beginTransaction();
52. ft.add(R.id.realtabcontent, Fragment11.newInstance(index) ,index+"");//将得到的fragment 替换当前的viewGroup内容,add则不替换会依次累加
53. ft.setTransition(Fragment www.hnnedu.com Transaction.TRANSIT_FRAGMENT_FADE);//设置动画效果
54. ft.commit();//提交
55. }
56.
57. @Override
58. public void onClick(View v) {
59.
60. allNoSelect();
61.
62. switch (v.getId()) {
63. case R.id.button1:
64. placeView(0);
65. break;
66. case R.id.button2:
67. placeView(1);
68. break;
69. case R.id.button3:
70. placeView(2);
71. break;
72. default:
73. break;
74. }
75.
76. v.setSelected(true);
77. }
78.
79. private void allNoSelect() {
80. layoutView.findViewById(R.id.button1).setSelected(false);
81. layoutView.findViewById(R.id.button2).setSelected(false);
82. layoutView.findViewById(R.id.button3).setSelected(false);
83. }
84.
85. public void placeView(int index){
86. BaseFragment fragment = (BaseFragment)"color: #ff0000;">getChildFragmentManager().findFragmentByTag(index+"");// getActivity().getSupportFragmentManager().findFragmentByTag(index+"");
87. //得到一个fragment 事务(类似sqlite的操作)
88. FragmentTransaction ft = getChildFragmentManager().beginTransaction();
89. if (fragment == null ) {
90. switch (index) {
91. case 0:
92. fragment = Fragment11.newInstance(index);
93. return;
94. case 1:
95. fragment = Fragment11.newInstance(index);
96. break;
97. case 2:
98. fragment = Fragment11.newInstance(index);
99. break;
100. default:
101.
102. return;
103. }
104.
105. }
106. ft.replace(R.id.realtabcontent, fragment,index+"");//将得到的fragment 替换当前的viewGroup内容,add则不替换会依次累加
107. ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//设置动画效果
108. ft.addToBackStack(null);
109. ft.commit();//提交
110.
111. }
112. }
Fragment翻译过来叫碎片,但是我个人觉得可以理解为具有生命周期的View
由于Fragment是3.0以后的东西,对于适应低版本,那我们只能使用谷歌官方提供的android-support-v4.jar这个包了,最快捷的办法就是右击工程名,选择Android Tools——add support library...,即可添加这个包了
由于3.0以下的Activity里面没有对Fragment的支持,所以选择继承android-support-v4.jar包中的FragmentActivity,其功能跟3.0及以后的版本的Activity的功能一样
DEMO下载:
FragmentTest.zip
底部导航加顶部导航,使用FragmentTabHost,Fragmetn,FragmentActivity组成,代码已经打包上传了
1、首先,底部导航的实现,FragmentTabHost和以前的TabHost的区别就是增加了对Fragment的支持,这里的实现和其他使用TabActivity是一样的,只是选项卡的切换已经从activity变成fragment了
2、这里主要讲一下顶部导航的实现,为了让大家对fragment更好的理解,这里我没有采用FragmentTabHost,而是放了三个Button
[java] view plaincopyprint?
01. package com.yyb.fragmenttest;
02.
03. import android.os.Bundle;
04. import android.support.v4.app.FragmentTabHost;
05. import android.support.v4.app.FragmentTransaction;
06. import android.view.LayoutInflater;
07. import android.view.View;
08. import android.view.ViewGroup;
09. import android.view.View.OnClickListener;
10.
11. public class Fragment1 extends BaseFragment implements OnClickListener{
12.
13. public static BaseFragment newInstance(int index) {
14. BaseFragment fragment = new Fragment1();
15. Bundle args = new Bundle();
16. args.putInt("index", index);
17. fragment.setArguments(args);
18. fragment.setIndex(index);
19. return fragment;
20. }
21.
22. private View layoutView;
23. private FragmentTabHost mTabHost;
24.
25. @Override
26. public void onCreate(Bundle savedInstanceState) {
27. super.onCreate(savedInstanceState);
28. }
29.
30. @Override
31. public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
32.
33. layoutView = inflater.inflate(R.layout.fragment_tabs, null);
34.
35. layoutView.findViewById(R.id.button1).setOnClickListener(this);
36. layoutView.findViewById(R.id.button1).setSelected(true);
37. layoutView.findViewById(R.id.button2).setOnClickListener(this);
38. layoutView.findViewById(R.id.button3).setOnClickListener(this);
39.
40. if(savedInstanceState ==null){
41. BaseFragment fragment = (BaseFragment)getChildFragmentManager().findFragmentByTag(0+"");// getActivity().getSupportFragmentManager().findFragmentByTag(index+"");
42. if(fragment==null){
43. init(0);
44. }
45. }
46.
47. return layoutView;
48. }
49.
50. private void init(int index) {
51. FragmentTransaction ft = getChildFragmentManager().beginTransaction();
52. ft.add(R.id.realtabcontent, Fragment11.newInstance(index) ,index+"");//将得到的fragment 替换当前的viewGroup内容,add则不替换会依次累加
53. ft.setTransition(Fragment www.hnnedu.com Transaction.TRANSIT_FRAGMENT_FADE);//设置动画效果
54. ft.commit();//提交
55. }
56.
57. @Override
58. public void onClick(View v) {
59.
60. allNoSelect();
61.
62. switch (v.getId()) {
63. case R.id.button1:
64. placeView(0);
65. break;
66. case R.id.button2:
67. placeView(1);
68. break;
69. case R.id.button3:
70. placeView(2);
71. break;
72. default:
73. break;
74. }
75.
76. v.setSelected(true);
77. }
78.
79. private void allNoSelect() {
80. layoutView.findViewById(R.id.button1).setSelected(false);
81. layoutView.findViewById(R.id.button2).setSelected(false);
82. layoutView.findViewById(R.id.button3).setSelected(false);
83. }
84.
85. public void placeView(int index){
86. BaseFragment fragment = (BaseFragment)"color: #ff0000;">getChildFragmentManager().findFragmentByTag(index+"");// getActivity().getSupportFragmentManager().findFragmentByTag(index+"");
87. //得到一个fragment 事务(类似sqlite的操作)
88. FragmentTransaction ft = getChildFragmentManager().beginTransaction();
89. if (fragment == null ) {
90. switch (index) {
91. case 0:
92. fragment = Fragment11.newInstance(index);
93. return;
94. case 1:
95. fragment = Fragment11.newInstance(index);
96. break;
97. case 2:
98. fragment = Fragment11.newInstance(index);
99. break;
100. default:
101.
102. return;
103. }
104.
105. }
106. ft.replace(R.id.realtabcontent, fragment,index+"");//将得到的fragment 替换当前的viewGroup内容,add则不替换会依次累加
107. ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//设置动画效果
108. ft.addToBackStack(null);
109. ft.commit();//提交
110.
111. }
112. }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询