Android ViewPager Fragment使用懒加载提升性能
Fragment在如今的Android开发中越来越普遍,但是当ViewPager结合Fragment时候,由于Android ViewPager内在的加载机制,导致一个比较严重的加载性能问题,具体来说,假设一个ViewPager中有n多个Fragment,那么ViewPager在初始化阶段将一次性的初始化FragmentPagerAdapter中的至少3个Fragment(如果Fragment多于3),创建和加载FragmentPagerAdapter
中Fragment中的View和数据。这个问题在以前轻量级app中不常见,而如今的APP,数据量越来越大,view越来越酷炫,这也意味着如果一次性全部创建全部这些Fragment,将导致比较严重的性能开销,处理不善将导致内存堆栈溢出而致使APP崩溃。
解决该问题的一个方案就是利用Fragment的setUserVisibleHint。
setUserVisibleHint的Android官方文档:
setUserVisibleHint void setUserVisibleHint (boolean isVisibleToUser) Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and is persistent across fragment instance state save and restore. An app may set this to false to indicate that the fragment's UI is scrolled out of visibility or is otherwise not directly visible to the user. This may be used by the system to prioritize operations such as fragment lifecycle updates or loader ordering behavior. Note: This method may be called outside of the fragment lifecycle. and thus has no ordering guarantees with regard to fragment lifecycle method calls. Parameters isVisibleToUser boolean: true if this fragment's UI is currently visible to the user (default), false if it is not.
我写的一个例子,说明Android中使用ViewPager加载Fragment使用懒加载的实例代码:
package zhangphil.app; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager pager = (ViewPager) findViewById(R.id.viewpager); ItemFragmentPagerAdapter adapter = new ItemFragmentPagerAdapter(this.getSupportFragmentManager()); pager.setAdapter(adapter); } public static class ItemFragment extends Fragment { private TextView text; @Override public void setUserVisibleHint(boolean isVisibleToUser) { Log.d("当前Fragment是否可见?", String.valueOf(isVisibleToUser)); if (isVisibleToUser) { //加载 loadData(); } else { } } //在这里假设一个Fragment需要加载很多数据很复杂很耗时的时间后才能获得足够数据渲染View private void loadData() { final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0xa1) { //加载完毕,赋值 text.setText("Fragment懒加载 zhangphil@csdn"); } } }; new Thread(new Runnable() { @Override public void run() { //假设此处是耗时操作 SystemClock.sleep(10000); handler.sendEmptyMessage(0xa1); } }).start(); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { text = (TextView) view.findViewById(android.R.id.text1); text.setText("加载中..."); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(android.R.layout.simple_list_item_1, null); } } private class ItemFragmentPagerAdapter extends FragmentPagerAdapter { public ItemFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return new ItemFragment(); } @Override public int getCount() { return 999; } } }