1. 程式人生 > >徹底解決Fragment重複載入問題,杜絕一切卡頓現象

徹底解決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(); } }