在項目中,都或多或少地使用的Tab佈局,因此大都會用到ViewPager+Fragment,可是Fragment有個很差或者太好的地方。例如你在ViewPager中添加了三個Fragment,當加載ViewPager中第一個Fragment時,它會默認幫你預先加載了第二個Fragment,當你加載第二個Fragment時,它會幫你加載第三個Fragment。這樣雖然有時很好,可是用戶只需看一個Fragment時,咱們就作了一些多餘工做加載了第二個Fragment。在這隻須要取消Fragment的預加載便可,只有當用戶切換到某個Fragment才加載..java
首先,介紹兩個方法void setUserVisibleHint(boolean isVisibleToUser)、boolean getUserVisibleHint(),它們分別用做設置/得到Fragment可見狀態,咱們能夠重寫Fragment在其中作判斷,代碼以下:android
import android.support.v4.app.Fragment; public abstract class BaseFragment extends Fragment { /** Fragment當前狀態是否可見 */ protected boolean isVisible; @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if(getUserVisibleHint()) { isVisible = true; onVisible(); } else { isVisible = false; onInvisible(); } } /** * 可見 */ protected void onVisible() { lazyLoad(); } /** * 不可見 */ protected void onInvisible() { } /** * 延遲加載 * 子類必須重寫此方法 */ protected abstract void lazyLoad(); }
在咱們的Fragment中,只須要繼承這個類,而後重寫其中的lazyLoad()方法,當Fragment對用戶可見(即用戶切換到此Fragment時)咱們在lazyLoad()中加載所需數據,詳細代碼看下面,我寫了個假的獲取數據線程:app
import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class CustomListFragment extends BaseFragment { private static final String FRAGMENT_INDEX = fragment_index; private final int FIRST_FRAGMENT = 0; private final int SECOND_FRAGMENT = 1; private final int THIRD_FRAGMENT = 2; private TextView mFragmentView; private int mCurIndex = -1; /** 標誌位,標誌已經初始化完成 */ private boolean isPrepared; /** 是否已被加載過一次,第二次就再也不去請求數據了 */ private boolean mHasLoadedOnce; /** * 建立新實例 * * @param index * @return */ public static CustomListFragment newInstance(int index) { Bundle bundle = new Bundle(); bundle.putInt(FRAGMENT_INDEX, index); CustomListFragment fragment = new CustomListFragment(); fragment.setArguments(bundle); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if(mFragmentView == null) { mFragmentView = (TextView) inflater.inflate(R.layout.fragment, container, false); //得到索引值 Bundle bundle = getArguments(); if (bundle != null) { mCurIndex = bundle.getInt(FRAGMENT_INDEX); } isPrepared = true; lazyLoad(); } //由於共用一個Fragment視圖,因此當前這個視圖已被加載到Activity中,必須先清除後再加入Activity ViewGroup parent = (ViewGroup)mFragmentView.getParent(); if(parent != null) { parent.removeView(mFragmentView); } return mFragmentView; } @Override protected void lazyLoad() { if (!isPrepared || !isVisible || mHasLoadedOnce) { return; } new AsyncTask<void, boolean="">() { @Override protected void onPreExecute() { super.onPreExecute(); //顯示加載進度對話框 UIHelper.showDialogForLoading(getActivity(), 正在加載..., true); } @Override protected Boolean doInBackground(Void... params) { try { Thread.sleep(2000); //在這裏添加調用接口獲取數據的代碼 //doSomething() } catch (Exception e) { e.printStackTrace(); } return true; } @Override protected void onPostExecute(Boolean isSuccess) { if (isSuccess) { // 加載成功 setView(); mHasLoadedOnce = true; } else { // 加載失敗 } //關閉對話框 UIHelper.hideDialogForLoading(); } }.execute(); } private void setView() { // 根據索引加載不一樣視圖 switch (mCurIndex) { case FIRST_FRAGMENT: mFragmentView.setText(第一個); break; case SECOND_FRAGMENT: mFragmentView.setText(第二個); break; case THIRD_FRAGMENT: mFragmentView.setText(第三個); break; } } }
到這裏咱們只是寫好了Fragment,在FragmentActivity中還須要對ViewPager設置一下,讓它每次只加載一個Fragment,ViewPager.setOffscreenPageLimit(int limit),其中參數能夠設爲0或者1,參數小於1時,會默認用1來做爲參數,未設置以前,ViewPager會默認加載兩個Fragment。因此,咱們只須要調用下它,設置下加載Fragment個數便可。ide
import java.util.ArrayList; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.RadioButton; import android.os.Bundle; public class MainActivity extends FragmentActivity implements OnClickListener{ private RadioButton mFstBtn; private RadioButton mSndBtn; private RadioButton mThdBtn; private ViewPager mViewPager; private ListFragmentPagerAdapter mPagerAdapter; private List<fragment> mFragments = new ArrayList<fragment>(); private final int FIRST_FRAGMENT = 0; private final int SECOND_FRAGMENT = 1; private final int THIRD_FRAGMENT = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initButton(); initViewPager(); } /** * 初始化按鈕 */ private void initButton() { mFstBtn = (RadioButton)findViewById(R.id.id_rb_fst); mFstBtn.setOnClickListener(this); mSndBtn = (RadioButton)findViewById(R.id.id_rb_snd); mSndBtn.setOnClickListener(this); mThdBtn = (RadioButton)findViewById(R.id.id_rb_thd); mThdBtn.setOnClickListener(this); } /** * 初始化ViewPager控件 */ private void initViewPager() { mViewPager = (ViewPager)findViewById(R.id.id_vp_viewpager); //關閉預加載,默認一次只加載一個Fragment mViewPager.setOffscreenPageLimit(1); //添加Fragment mFragments.add(CustomListFragment.newInstance(FIRST_FRAGMENT)); mFragments.add(CustomListFragment.newInstance(SECOND_FRAGMENT)); mFragments.add(CustomListFragment.newInstance(THIRD_FRAGMENT)); //適配器 mPagerAdapter = new ListFragmentPagerAdapter(getSupportFragmentManager(), mFragments); mViewPager.setAdapter(mPagerAdapter); mViewPager.setOnPageChangeListener(onPageChangeListener); } private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() { @Override public void onPageSelected(int position) { //根據用戶選中的按鈕修改按鈕樣式 switch (position) { case FIRST_FRAGMENT: mFstBtn.setChecked(true); mSndBtn.setChecked(false); mThdBtn.setChecked(false); break; case SECOND_FRAGMENT: mFstBtn.setChecked(false); mSndBtn.setChecked(true); mThdBtn.setChecked(false); break; case THIRD_FRAGMENT: mFstBtn.setChecked(false); mSndBtn.setChecked(false); mThdBtn.setChecked(true); break; } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) {} @Override public void onPageScrollStateChanged(int arg0) {} }; @Override public void onClick(View v) { switch (v.getId()) { case R.id.id_rb_fst: mViewPager.setCurrentItem(FIRST_FRAGMENT); break; case R.id.id_rb_snd: mViewPager.setCurrentItem(SECOND_FRAGMENT); break; case R.id.id_rb_thd: mViewPager.setCurrentItem(THIRD_FRAGMENT); break; } } }