Having a base class (which is Parceable) using Builder pattern, now would like to create a child class derived from it so that override the default customFunc_1
and customFunc_2
function implementation.
If simply deriving from the base class,
class DerivedDataConfig : BaseDataConfig {
override open fun customFunc_1(context: Context, savedInstanceState: Bundle?,
onElementClickListener: ElementClickListener? = null) : FrameLayout? {
// differnt than base
Log.i("+++", "+++, customFunc_1 called in derived class")
return android.widget.FrameLayout(context)
}
override fun customFunc_2(viewToBind: View, content: IData, position: Int) {
Log.i("+++", "+++, customFunc_2 called in derived class")
}
}
after put in the bundle and getParcelbale from the bundle,
bundle.putParcelable(KEY_DATA_CONFIG, derivedDataConfig)
var derivedDataConfig.getParcelable(KEY_DATA_CONFIG)
it cast back to the base class (lost the overridden function implementation from the derived class)
How to do it in kotlin to derive from a base class which Praceable?
If there is no way to reuse the base class's builder it is ok, but seems having problem to derive from a parent which is Parcelable.
Anyone knows how to do it?
open class BaseDataConfig() : Parcelable {
var param_1 = false
var param_2 = ArrayList<DataDescriptor>()
private constructor(parcel: Parcel) : this() {
param_1 = parcel.readByte() != 0.toByte()
parcel.readList(param_1, DataDescriptor::class.java.classLoader)
}
open fun customFunc_1(context: Context, savedInstanceState: Bundle?,
onElementClickListener: ElementClickListener? = null) : FrameLayout? {
return null
}
open fun customFunc_2(viewToBind: View, content: IData, position: Int) {
}
class Builder {
private var param_1 = false
private var param_2 = ArrayList<DataDescriptor>()
fun setParam_1(b: Boolean) = apply { this.param_1 = b }
fun setParam_2(type: String, id: Int) = apply { this.param_2.add(DataDescriptor(type, id)) }
fun build() : DataConfig {
return DataConfig().also {
it.param_1 = param_1
it.param_1_2 = param_2
}
}
}
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeByte(if (param_1) 1 else 0)
dest.writeList(param_2)
}
override fun describeContents(): Int {
return 0
}
companion object {
@JvmField
val CREATOR: Parcelable.Creator<DataConfig> = object : Parcelable.Creator<DataConfig> {
override fun createFromParcel(parcel: Parcel): DataConfig {
return DataConfig(parcel)
}
override fun newArray(size: Int): Array<DataConfig?> {
return arrayOfNulls(size)
}
}
}
}
class DataDescriptor(val type: String, val id: Int)