王学岗Fragment(四)

使用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);
                }
            }

        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值