0

I have this block of code:

val page = object : AbstractViewRenderer(this, R.layout.pdf_page) {
    private var title: String? = null

    override fun initView(view: View) {}
}

Now I want to make a mutable list of object : AbstractViewRenderer(this, R.layout.pdf_page).

I mean something like this:

val page[] : MutableList<object : AbstractViewRenderer> = ...

How to do it? What should I use?

1 Answer 1

1

Using an object expression creates a one-time, anonymous instance. If you want to reuse the logic inside it, create a class for it, something like this:

class MyViewRenderer(ctx: Context, layoutResId: Int) : AbstractViewRenderer(ctx, layoutResId) {
    private var title: String? = null

    override fun initView(view: View) {}
}

Then you can create instances or lists of instances of that class:

val page = MyViewRenderer(this, R.layout.pdf_page)

val pages: MutableList<AbstractViewRenderer> = mutableListOf(
        MyViewRenderer(this, R.layout.pdf_page),
        MyViewRenderer(this, R.layout.pdf_page),
        MyViewRenderer(this, R.layout.pdf_page)
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.