zoukankan      html  css  js  c++  java
  • Android ViewPager Fragment使用懒加载提升性能

    

    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;
            }
        }
    }


  • 相关阅读:
    Java集合框架--List 遍历
    Java集合框架--List 类
    Leetcode 239 Sliding Window Maximum (指定滑动窗最大值) (滑动窗口)
    Vim配置
    子字符串模板 (双指针, 滑动窗口)
    Leetcode 76 Minimum Window Substring. (最小窗口子字符串) (滑动窗口, 双指针)
    Leetcode 3 Longest Substring Without Repeating Characters. (最长无重复字符子串) (滑动窗口, 双指针)
    一切从赞美开始
    Leetcode 10 regular expression matching (正则表达式匹配) (动态规划)
    Leetcode 5 Longest Palindromic Substring (最长回文子字符串)(动态规划)
  • 原文地址:https://www.cnblogs.com/hehehaha/p/6147238.html
Copyright © 2011-2022 走看看