带生命周期的View

自从谷歌推出了新的生命周期组件我们的开发可以更方便 更解耦了 对于下面的这样的视图 可以更简单的拆分解耦复用了

里面有三块不同的视图,常规做法就是写个scrollview 控件全部写进去,然后 请求多个网络数据填充数据 这样先不说Activity/prestener 代码量会很大,而且假如绿色那一块在很多Activity里面重复,这样的话很难达到复用的目的,如果这三个小块,每个都能有自己的生命周期,各自管理各自的数据,每个都是一个带生命周期的View的话那就解决了代码量大,难以复用的情况(Fragment个人感觉有些重) 代码很简单

open abstract class BaseLifeCycleView : FrameLayout, LifecycleObserver { //实现生命周期库的LifecycleObserver接口

    protected abstract val contentViewLayoutID: Int

    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context)
    }

    @CallSuper
    fun init(mContext: Context) { //因为Activity已经实现了 Lifecycle相关接口 所以这里加入监听 也是此类的核心代码
        if (mContext is SupportActivity) {
            val mLifecycleOwner = mContext as LifecycleOwner
            mLifecycleOwner.lifecycle.addObserver(this)
        }
    }
    
    @CallSuper
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
   open fun onCreate() {
        val mView = LayoutInflater.from(context).inflate(contentViewLayoutID, this, false) //加入类似Activity的设置布局
        addView(mView)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    open fun onStart() {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    open fun onResume() {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    open fun onStop() {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    open fun onDestory() {
    }

}

复制代码

然后你的View 继承这个基类就行啦,比如


class ViewOne @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseLifeCycleView(context, attrs, defStyleAttr) {
    override val contentViewLayoutID: Int
        get() = R.layout.myview

    override fun onCreate() {
        super.onCreate()
        //网络请求  todo something
        Http.getDefault().today()
                .get(context) {
                    //todo   setText  setImage  etc..
                }
    }
    
}
复制代码

所以回到一开始的布局文件,里面有三个小模块 我们可以写三个view 然后直接写入xml布局就行啦。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <com.example.admin.kotlintest.ViewOne
        android:background="@color/red_color"
        android:layout_width="match_parent"
        android:layout_height="200dp">

    </com.example.admin.kotlintest.ViewOne>
    
    <com.example.admin.kotlintest.ViewTwo
        android:background="@color/green_color"
        android:layout_width="match_parent"
        android:layout_height="200dp">

    </com.example.admin.kotlintest.ViewTwo>
    
    <com.example.admin.kotlintest.ViewThree
        android:background="@color/gray"
        android:layout_width="match_parent"
        android:layout_height="200dp">

    </com.example.admin.kotlintest.ViewThree>
    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
复制代码

各种复用无压力 ,各个view拥有独自的生命周期,至于view之间的通信 有多种方式,liveData,接口回调等等,甚至eventbus,使用自己熟悉的方式就行~ 这样,就一个很简单的类,我们解决了一个复杂界面的模块化的问题,可以高度重用,更加容易维护。

转载于:https://juejin.im/post/5c13346be51d452e6a4e523b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值