Android 封装一个属于自己的BaseActivity父类和BaseFragment父类

我们在使用Activity的过程中,基本会重复一部分代码,今天我们写一个我们自己的统一父类,便于我们开发使用

注释:

这里继承于RxAppCompatActivity()和实现CustomAdapt用于适配
重写方法isBaseOnWidth()和getSizeInDp()
具体使用点击查看

一: BaseActivity

第一步:创建BaseActivity

abstract class BaseActivity : RxAppCompatActivity(), CustomAdapt {

    lateinit var mActivity: Activity

    override fun isBaseOnWidth(): Boolean {
        return false
    }

    override fun getSizeInDp(): Float {
        return 640F
    }
 	abstract val layout: Int
 	
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layout)
        mActivity = this
        initWindow()
        init()
        request()
        initIntent()
        initPresenter()
        initView()
        initEvent()
        getData()
        initVisible()
    }

    private fun init() {
        App.getInstance().registerActivity(this)
    }

    open fun initIntent() {}
    private fun initWindow() { }
    open fun initView() {}
    open fun initEvent() {}
    open fun getData() {}
    open fun initPresenter() {}
    open fun initVisible() {}
	override fun onBackPressed() {}

    override fun onDestroy() {
        super.onDestroy()
        App.getInstance().unregisterActivity(this)
    }

    fun initTitle(title: String, bl_back: Boolean, right: String) {
        title_name.text = title
        if (bl_back)
            title_back.visibility = View.VISIBLE
        if (right.isNotEmpty()) {
            title_right.visibility = View.VISIBLE
            title_right.text = right
        }

        title_back.setOnClickListener { finish() }

    }

    protected open fun showToast(s: String) {
        ToastUtils.showShort(s)
    }

}

第二步:创建MainActivity继承于BaseActivity

class MainActivity : BaseActivity() {
    override val layout = R.layout.a_main
}

二: BaseFragment

abstract class BaseFragment : RxFragment() {

    lateinit var mContext: Context
    private var rootView: View? = null
    private var isViewPrepared = false  //标识fragment视图已经初始化完毕
    private var hasFetchData = false  //标识已经触发过懒加载数据

    override fun onAttach(mContext: Context) {
        super.onAttach(mContext)
        this.mContext = mContext
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        rootView?.let {
            (it.parent as ViewGroup?)?.removeView(it)
        } ?: let {
            initIntent(arguments)
            rootView = inflater.inflate(layout, container, false)
        }
        return rootView
    }

    open fun initIntent(arguments: Bundle?) {}

    abstract val layout: Int

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        initPresenter()
        initView()
        initEvent()
        initType()

    }

    override fun onResume() {
        super.onResume()
        getData()
    }

    open fun getData() {}
    open fun initType() {}
    open fun initView() {}

    open fun initEvent() {}

    open fun initPresenter() {}

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        isViewPrepared = true
        lazyFetchDataIfPrepared()
  
    }

    private fun lazyFetchDataIfPrepared() {
        //用户可见fragment && 没有加载过数据 && 视图已经准备完毕
        if (!hasFetchData && isViewPrepared) {
            hasFetchData = true
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        //view被销毁后,将可以重新触发数据懒加载,因为在viewpager下,fragment不会再次新建并走onCreate的生命周期流程,将从onCreateView开始
        hasFetchData = false
        isViewPrepared = false
    }


    // fun getProvider() = mContext as BaseActivity
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值