彻底解决Fragment重复加载问题,杜绝一切卡顿现象

Fragment的重复加载问题。之前在一些小项目中经常用到Fragment,一般都使用fragmentTransaction.replace(R.id.content,user_Fa)去动态加载布局。因为之前项目小,user_fa该fragment数据量比较少,对整体项目并无影响所以就没去管,直到前天,遇到加载的fragment数据量比较大。切换fragment时出现卡顿现象,分析其原因竟然是每次调用replace都会去加载一次fragment,而此时的fragment又好大,所以就出现卡顿现象,加载一次fragment说白了就是每次replace都会调用onCreateView方法,要是不信你可以打Log测试一下,那碰到这卡顿怎么办勒,还记得fragment给我们提供了另外的方法吗?add和show方法可以帮我解决这个问题,看代码吧 

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import Fragment.SheZhiFragment;
import Fragment.ShiPinFragment;
import Fragment.ShiTiFragment;
import Fragment.ShouYeFragment;
import base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.update.BmobUpdateAgent;

public class MainActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.fanhui_btn)
    ImageView fanhuiBtn;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.edit)
    ImageView edit;
    @BindView(R.id.shiti)
    RadioButton shiti;
    @BindView(R.id.shipin)
    RadioButton shipin;
    @BindView(R.id.shezhi)
    RadioButton shezhi;
    @BindView(R.id.fragmentcontent)
    FrameLayout fragmentcontent;
    @BindView(R.id.shouye)
    RadioButton shouye;
    private ShiTiFragment shitifragment = null;
    private ShiPinFragment shipinfragment = null;
    private SheZhiFragment shezhifragment = null;
    private ShouYeFragment shouyefragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BmobUpdateAgent.update(this);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setDefaultFragment();
        shiti.setOnClickListener(this);
        shipin.setOnClickListener(this);
        shezhi.setOnClickListener(this);
        shouye.setOnClickListener(this);
        fanhuiBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        switch (v.getId()) {

            case R.id.shouye:
                textTitle.setText("首页");
                if (shouyefragment == null) {
                    shouyefragment = new ShouYeFragment();
                }
                //如果tab2不为空,把tab2隐藏就是、
                if (shitifragment != null) {
                    transaction.hide(shitifragment);
                }
                if (shipinfragment != null) {
                    transaction.hide(shipinfragment);
                }
                if (shezhifragment != null) {
                    transaction.hide(shezhifragment);
                }
                if (!shouyefragment.isAdded()) {

                    transaction.add(R.id.fragmentcontent, shouyefragment);
                } else {
                    transaction.show(shouyefragment);
                }
                break;
            case R.id.shiti:
                textTitle.setText("C语言试题");
                if (shitifragment == null) {
                    shitifragment = new ShiTiFragment();
                }
                //如果tab2不为空,把tab2隐藏就是、
                if (shouyefragment != null) {
                    transaction.hide(shouyefragment);
                }
                if (shipinfragment != null) {
                    transaction.hide(shipinfragment);
                }
                if (shezhifragment != null) {
                    transaction.hide(shezhifragment);
                }
                if (!shitifragment.isAdded()) {

                    transaction.add(R.id.fragmentcontent, shitifragment);
                } else {
                    transaction.show(shitifragment);
                }
                break;
            case R.id.shipin:
                textTitle.setText("C语言视频");
                if (shipinfragment == null) {
                    shipinfragment = new ShiPinFragment();
                }
                //如果tab2不为空,把tab2隐藏就是、
                if (shouyefragment != null) {
                    transaction.hide(shouyefragment);
                }
                if (shitifragment != null) {
                    transaction.hide(shitifragment);
                }
                if (shezhifragment != null) {
                    transaction.hide(shezhifragment);
                }
                if (!shipinfragment.isAdded()) {

                    transaction.add(R.id.fragmentcontent, shipinfragment);
                } else {
                    transaction.show(shipinfragment);
                }
                break;
            case R.id.shezhi:
                textTitle.setText("设置");
                if (shezhifragment == null) {
                    shezhifragment = new SheZhiFragment();
                }
                //如果tab2不为空,把tab2隐藏就是、
                if (shouyefragment != null) {
                    transaction.hide(shouyefragment);
                }
                if (shitifragment != null) {
                    transaction.hide(shitifragment);
                }
                if (shipinfragment != null) {
                    transaction.hide(shipinfragment);
                }
                if (!shezhifragment.isAdded()) {

                    transaction.add(R.id.fragmentcontent, shezhifragment);
                } else {
                    transaction.show(shezhifragment);
                }
                break;
            case R.id.fanhui_btn:
                finish();
                break;
        }
        transaction.commit();
    }

    private void setDefaultFragment() {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (shouyefragment == null) {
            shouyefragment = new ShouYeFragment();
        }
        //如果tab2不为空,把tab2隐藏就是、
        if (shipinfragment != null) {
            transaction.hide(shipinfragment);
        }
        if (shitifragment != null) {
            transaction.hide(shitifragment);
        }
        if (shezhifragment != null) {
            transaction.hide(shezhifragment);
        }
        if (!shouyefragment.isAdded()) {

            transaction.add(R.id.fragmentcontent, shouyefragment);
        } else {
            transaction.show(shouyefragment);
        }

        transaction.commit();
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值