In kotlin how to make a derived class from a Parcelable parent (and has implemented a nested Builder class)?

▼魔方 西西 提交于 2019-12-11 17:48:55

问题


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)

回答1:


Find the solution for derive from a Parcelable parent class at here copy here for ref:

class SavedState extends BaseSavedState {
    int stateToSave;

    SavedState(Parcelable superState) {
      super(superState);
    }

    private SavedState(Parcel in) {
      super(in);
      this.stateToSave = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
      super.writeToParcel(out, flags);
      out.writeInt(this.stateToSave);
    }

    //required field that makes Parcelables from a Parcel
    public static final Parcelable.Creator<SavedState> CREATOR =
        new Parcelable.Creator<SavedState>() {
          public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
          }
          public SavedState[] newArray(int size) {
            return new SavedState[size];
          }
    };
}

And I guess for the Builder it may have to add a nested Builder class in the derived class. Or anyone has better way?



来源:https://stackoverflow.com/questions/54621165/in-kotlin-how-to-make-a-derived-class-from-a-parcelable-parent-and-has-implemen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!