使用replace替换原来的fragment会频繁的销毁和创建 ,导致垃圾回收机制性能下降。我们在(四中),使用add替换replace
把 MyMenuFragment修改如下
package com.tz.katefragement_repalce;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
// ListFragment :Fragment+ListView
//ontach---oncreat---onCreatView--onActivityCreate
public class MyMenuFragment extends ListFragment implements OnItemClickListener {
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday", };
@Override
public void onActivityCreated(Bundle savedInstanceState) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, days);
this.setListAdapter(adapter);
getListView().setOnItemClickListener(this);
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
FragmentManager manager=getActivity().getSupportFragmentManager();
//已添加的fragment里面有没有需要的fragment,如果有直接显示
MyContentFragment old=(MyContentFragment) manager.findFragmentByTag("fragment"+position);
if(old==null){
MyContentFragment fragment=MyContentFragment.getInstance(position);
FragmentTransaction transaction=manager.beginTransaction();
//第三个参数可以为我们的Fragment设置一个别名
// transaction.replace(R.id.fl_content, fragment,"Fragment"+position);
//添加并没有把原来的给删掉,不同于replace的频繁销毁原来的fragment
transaction.add(R.id.fl_content,fragment, "fragment"+position);
transaction.show(fragment);
transaction.commit();
}else{
FragmentTransaction transaction2=manager.beginTransaction();
transaction2.show(old);
transaction2.commit();
}
}
}
但是这样也有问题,就是你说点击的Fragment虽然显示了,但原来的Fragmnt却并没有消失。会出现下面这种情况。
我们还要将原来的Fragment隐藏。
我们修改 MyMenuFragment
package com.tz.katefragement_repalce;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
// ListFragment :Fragment+ListView
//ontach---oncreat---onCreatView--onActivityCreate
public class MyMenuFragment extends ListFragment implements OnItemClickListener {
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday", };
@Override
public void onActivityCreated(Bundle savedInstanceState) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, days);
this.setListAdapter(adapter);
getListView().setOnItemClickListener(this);
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
FragmentManager manager=getActivity().getSupportFragmentManager();
//已添加的fragment里面有没有需要的fragment,如果有直接显示
MyContentFragment old=(MyContentFragment) manager.findFragmentByTag("fragment"+position);
if(old==null){
MyContentFragment fragment=MyContentFragment.getInstance(position);
FragmentTransaction transaction=manager.beginTransaction();
//第三个参数可以为我们的Fragment设置一个别名
// transaction.replace(R.id.fl_content, fragment,"Fragment"+position);
//添加并没有把原来的给删掉,不同于replace的频繁销毁原来的fragment
transaction.add(R.id.fl_content,fragment, "fragment"+position);
checkOldFragments(position, manager,
transaction);
transaction.commit();
}else{
FragmentTransaction transaction2=manager.beginTransaction();
checkOldFragments(position, manager, transaction2);
//隐藏其他的Fragment
transaction2.commit();
}
}
private void checkOldFragments(int position, FragmentManager manager,
FragmentTransaction transaction2) {
for(int i=0;i<days.length;i++){
MyContentFragment mcf2=(MyContentFragment) manager.findFragmentByTag("fragment"+i);
if(mcf2!=null){
if(i==position){
transaction2.show(mcf2);
}else{
transaction2.hide(mcf2);
}
}
}
}
}