mport java.util.List;
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.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.tz.dream.fragment.tabbar.fragment.BaseFragment;
import com.tz.dream.fragment.tabbar.fragment.MainFragment;
import com.tz.dream.fragment.tabbar.fragment.MeFragment;
import com.tz.dream.fragment.tabbar.fragment.MoreFragment;
import com.tz.dream.fragment.tabbar.fragment.PointFragment;
public class MainActivity extends FragmentActivity implements
OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(this);
// 如果呼叫了此方法那麼onCheckedChanged會呼叫兩次
radioGroup.check(R.id.rb_main);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Toast.makeText(this, "onCheckedChanged", Toast.LENGTH_SHORT).show();
// 替換fragment
// 一般比較粗糙的辦法
// 思考:這麼幹有什麼不好的?
// 每次都要建立物件---這樣的話會導致我們的GC垃圾回收器不斷的回收我們的物件
// 所以說我們不能每一次都建立fragment
switch (checkedId) {
case R.id.rb_main:
show(MainFragment.class);
// beginTransaction.add(arg0, arg1)
// beginTransaction.remove(arg0)
// beginTransaction.show(arg0)
// beginTransaction.hide(arg0)
// beginTransaction.replace(R.id.fl, new MainFragment());
// beginTransaction.commit();
break;
case R.id.rb_me:
show(MeFragment.class);
// beginTransaction.replace(R.id.fl, new MeFragment());
// beginTransaction.commit();
break;
case R.id.rb_point:
show(PointFragment.class);
// beginTransaction.replace(R.id.fl, new PointFragment());
// beginTransaction.commit();
break;
case R.id.rb_more:
show(MoreFragment.class);
// beginTransaction.replace(R.id.fl, new MoreFragment());
// beginTransaction.commit();
break;
default:
break;
}
}
/**
* 顯示fragment 在這裡我的fragment的tag都是我們的類名
*
* @param clazz
*/
private void show(Class<? extends BaseFragment> clazz) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = fragmentManager
.beginTransaction();
// 先找我們的fragment
Fragment fragment = fragmentManager.findFragmentByTag(clazz
.getSimpleName());
if (fragment == null) {
try {
fragment = clazz.newInstance();
beginTransaction.add(R.id.fl, fragment, fragment.getClass()
.getSimpleName());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
// 顯示當前選中的fragment
List<Fragment> fragments = fragmentManager.getFragments();
// 第一次獲取fragment數量為0
if (fragments != null) {
for (Fragment fragment2 : fragments) {
if (isTabFragment(fragment2)) {
// 我們主頁面需要fragment
if (fragment2.getClass().getSimpleName()
.equals(clazz.getSimpleName())) {
beginTransaction.show(fragment2);
} else {
beginTransaction.hide(fragment2);
}
}
}
} else {
// 等於null的時候我們預設顯示第一項
beginTransaction.show(fragment);
}
beginTransaction.commit();
}
// 篩選tab 的fragment
private boolean isTabFragment(Fragment fragment) {
String fragmentTag = fragment.getClass().getSimpleName();
if (fragmentTag.equals(MainFragment.class.getSimpleName())
|| fragmentTag.equals(MeFragment.class.getSimpleName())
|| fragmentTag.equals(PointFragment.class.getSimpleName())
|| fragmentTag.equals(MoreFragment.class.getSimpleName())) {
return true;
}
return false;
}
}
整理自教程