标签:
主要利用了Fragment在主内容界面
对Fragment的add,hide等事务操作。(FragmentTraction )
注意:不要使用一个全局事务: FragmentTrasction 提交。
package com.example.tabdemo01; 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.TextView; public class MainActivity extends FragmentActivity { TextView tv_header; FragmentManager fmManager ; Fragment fm1; Fragment fm2; Fragment fm3; Fragment fm4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //将所有的Fragment 隐藏 onweixin(null); } public void initView() { tv_header = (TextView) findViewById(R.id.tv_headr); fmManager = getSupportFragmentManager(); } public void onweixin(View view){ tv_header.setText("微信"); } public void onFriend(View view){ tv_header.setText("好友"); } public void onContacts(View view){ tv_header.setText("联系人"); } public void onMyself(View view){ tv_header.setText("我的"); } public void selectionTab(int index) { FragmentTransaction ft = fmManager.beginTransaction(); //将所有的Fragment 隐藏 hideFragments(ft, fm1, fm2, fm3, fm4); switch (index) { case 0: if(fm1 == null){ fm1 = new Fragment01(); ft.add(R.id.id_content, fm1); }else { ft.show(fm1); } break; case 1: if(fm2 == null){ fm2 = new Fragment02(); ft.add(R.id.id_content, fm2); }else { ft.show(fm2); } break; case 2: if(fm3 == null){ fm3 = new Fragment03(); ft.add(R.id.id_content, fm3); }else { ft.show(fm3); } break; case 3: if(fm4 == null){ fm4 = new Fragment04(); ft.add(R.id.id_content, fm4); }else { ft.show(fm4); } break; default: break; } ft.commit(); } /** * 将制定fragment 隐藏Fragment * @param fragment */ public void hideFragments(FragmentTransaction ft, Fragment...fragment){ for (Fragment fragment2 : fragment) { if(fragment2 != null){ ft.hide(fragment2); } } } }
下面的代码出现异常, 导致切换界面时, 应用闪退。
在代码中, FragmentTrasaction ft是一个全局变量,
但是在, 点击相应事件中,调用了多次,
该错误,是因为你的ft事务是全局的变量,只能commit一次。
package com.example.tabdemo01; 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.TextView; public class MainActivity extends FragmentActivity { TextView tv_header; FragmentManager fmManager ; FragmentTransaction ft; Fragment fm1; Fragment fm2; Fragment fm3; Fragment fm4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //将所有的Fragment 隐藏 hideFragments(fm1, fm2, fm3, fm4); } public void initView() { tv_header = (TextView) findViewById(R.id.tv_headr); fmManager = getSupportFragmentManager(); /* fm1 = new Fragment01(); fm2 = new Fragment02(); fm3 = new Fragment03(); fm4 = new Fragment04();*/ ft = fmManager.beginTransaction(); } public void onweixin(View view){ tv_header.setText("微信"); //将所有的Fragment 隐藏 hideFragments(fm1, fm2, fm3, fm4); if(fm1 == null){ fm1 = new Fragment01(); ft.add(R.id.id_content, fm1); }else { ft.show(fm1); } ft.commit(); } public void onFriend(View view){ //将所有的Fragment 隐藏 hideFragments(fm1, fm2, fm3, fm4); if(fm2 == null){ fm2 = new Fragment02(); ft.add(R.id.id_content, fm2); }else { ft.show(fm2); } tv_header.setText("好友"); ft.commit(); } public void onContacts(View view){ //将所有的Fragment 隐藏 hideFragments(fm1, fm2, fm3, fm4); tv_header.setText("联系人"); if(fm3 == null){ fm3 = new Fragment03(); ft.add(R.id.id_content, fm3); }else { ft.show(fm3); } ft.commit(); } public void onMyself(View view){ //将所有的Fragment 隐藏 hideFragments(fm1, fm2, fm3, fm4); tv_header.setText("我的"); if(fm4 == null){ fm4 = new Fragment04(); ft.add(R.id.id_content, fm4); }else { ft.show(fm4); } ft.commit(); } public void setTabSelection(int index, Fragment fm){ //将所有的Fragment 隐藏 hideFragments(fm1, fm2, fm3, fm4); } /** * 将制定fragment 隐藏Fragment * @param fragment */ public void hideFragments(Fragment...fragment ){ for (Fragment fragment2 : fragment) { if(fragment2 != null){ ft.hide(fragment2); } } } }
Tab系列 02 PagerAdapter+Fragment
标签:
原文地址:http://www.cnblogs.com/chengbao/p/5651623.html