美文网首页
android fragment懒加载

android fragment懒加载

作者: hao_developer | 来源:发表于2020-08-18 10:45 被阅读0次

    fragment基类

    open abstract class BaseLazyLoadFragment : Fragment() {
    
        private var isFirstLoad = false
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            val view = initView(inflater, container) //让子类实现初始化视图
            return view
        }
    
        override fun onActivityCreated(savedInstanceState: Bundle?) {
            super.onActivityCreated(savedInstanceState)
            initEvent()
            isFirstLoad = true //视图创建完成,将变量置为true
            if (userVisibleHint) { //如果Fragment可见进行数据加载
                onLazyLoad()
                isFirstLoad = false
            }
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
            isFirstLoad = false //视图销毁将变量置为false
        }
    
        override fun setUserVisibleHint(isVisibleToUser: Boolean) {
            super.setUserVisibleHint(isVisibleToUser)
            if (isFirstLoad && isVisibleToUser) { //视图变为可见并且是第一次加载
                onLazyLoad()
                isFirstLoad = false
            }
        }
    
        //数据加载接口,留给子类实现
        abstract fun onLazyLoad()
    
        //初始化视图接口,子类必须实现
        abstract fun initView(inflater: LayoutInflater?, container: ViewGroup?): View?
    
        //初始化事件接口,留给子类实现
        abstract fun initEvent()
    
        /**
         * 初始化View
         */
        protected open fun initView(){}
    
        /**
         * 初始化事件
         */
        protected open fun initListener(){}
    
        /**
         * 初始化Data
         */
        protected open fun initData(){}
    
    }
    

    实现fragment并继承基类

    class ShopOrderFragment : BaseLazyLoadFragment() {
    
        var paramsOne:String? = null
        var paramsTwo:String? = null
        var shopOrderAdapter:ShopOrderAdapter? = null
    
        fun newInstance(paramsOne:String,paramsTwo:String): ShopOrderFragment{
            val args = Bundle()
            args.putString(MyParms.PARAMS,paramsOne)
            args.putString(MyParms.PARAMS_OTHER,paramsTwo)
            val fragment = ShopOrderFragment()
            fragment.arguments = args
            return fragment
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            if (arguments != null){
                paramsOne = arguments?.getString(MyParms.PARAMS)
                paramsTwo = arguments?.getString(MyParms.PARAMS_OTHER)
                Log.e("fragment",paramsOne ?: "")
            }
        }
    
        override fun initView(inflater: LayoutInflater?, container: ViewGroup?): View? {
            return inflater?.inflate(R.layout.fragment_shop_order_view,container,false)
        }
    
        override fun initEvent() {
    
        }
    
        override fun onLazyLoad() {
            Log.e("fragment","$paramsOne 加载数据")
    
            shopOrderAdapter = ShopOrderAdapter(this.activity!!)
            val lm = LinearLayoutManager(context)
            lm.orientation = LinearLayoutManager.VERTICAL
            with(rvList){
                layoutManager = lm
                adapter = shopOrderAdapter
            }
        }
    
    
    
    }
    

    activity加载

    class ShopOrderActivity : BaseActivity(),View.OnClickListener {
    
        var fragmentList:ArrayList<Fragment>? = null
        var currentPosition = -1
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_shop_order_view)
            initView()
            initData()
            initListener()
        }
    
        override fun initView() {
            tvTitle.text = "店铺订单"
        }
    
        override fun initData() {
            currentPosition = intent.getIntExtra(MyParms.PARAMS,-1)
    
            fragmentList = arrayListOf()
            fragmentList?.add(ShopOrderFragment().newInstance("11","11"))
            fragmentList?.add(ShopOrderFragment().newInstance("22","22"))
            fragmentList?.add(ShopOrderFragment().newInstance("33","33"))
            fragmentList?.add(ShopOrderFragment().newInstance("44","44"))
            fragmentList?.add(ShopOrderFragment().newInstance("55","55"))
            fragmentList?.add(ShopOrderFragment().newInstance("66","66"))
    
            val tabStr = resources.getStringArray(R.array.shop_order_menu)
            tabLay.setViewPager(viewPager,tabStr,this,fragmentList)
            if (currentPosition >= 0)tabLay.currentTab = currentPosition
        }
    
        override fun initListener() {
            ivBack.setOnClickListener(this)
        }
    
        override fun onClick(v: View?) {
            when(v?.id){
                R.id.ivBack ->{//返回
                    finish()
                }
            }
        }
    
        override fun onDestroy() {
            super.onDestroy()
            if (fragmentList != null){
                fragmentList?.clear()
                fragmentList = null
            }
        }
    
    
    }
    

    使用的第三方插件

    implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
    

    相关文章

      网友评论

          本文标题:android fragment懒加载

          本文链接:https://www.haomeiwen.com/subject/sauhjktx.html