question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating Models From ViewHolders works in Java. Not in Kotlin

See original GitHub issue

I’ve been doing the following when creating Models from viewholders in java

@EpoxyModelClass(layout = R.layout.da_thing)
public abstract class MyModel extends EpoxyModelWithHolder<MyModel.MyHolder> {
    @EpoxyAttribute
    EmailItem emailItem;


    @Override
    public void bind(YourViewHolder holder) {
        super.bind(holder);
        holder.bindingTime(emailItem);
    }

    static class MyHolder extends BaseEpoxyHolder {

        @BindView(R.id.text) TextView textView;

        public void bindingTime(EmailItem emailItem) {
            textView(your_info_needed_for_model+"");

        }
    }
}

This works well, but when I convert to Kotlin (kotlin noob here) I get the following. It seems like the fault is mostly on Butterknife. The question is… since using Kotlin I do not use Butterknife anymore.

Essentially I got this though.

@EpoxyModelClass(layout = R.layout.da_thing)
abstract class MyModel : EpoxyModelWithHolder<MyModel.MyHolder>() {
    @EpoxyAttribute
    var emailItem: EmailItem = EmailItem()


    override fun bind(holder: MyHolder?) {
        super.bind(holder)
        holder!!.bindTo(emailItem)
    }

    class MyHolder : BaseEpoxyHolder() {

        @BindView(R.id.text) var textView: TextView? = null

        fun bindTo(emailItem: EmailItem) {
            textView?.setText("asdf")
        }
    }
}

It’d be great to get rid of butterknife in this case. Thoughts @elihart ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
elihartcommented, Aug 1, 2018

changing from annotationProcessor to kapt is the correct fix. I’m guessing you just needed to do a clean build to force it to recompile

1reaction
elihartcommented, Jul 28, 2018

@ColtonIdle Yes, you can shorten it to something like this

@EpoxyModelClass(layout = R.layout.view_holder_page_header)
abstract class SampleKotlinModelWithHolder : EpoxyModelWithHolder<Holder>() {

    @EpoxyAttribute lateinit var title: String
    @EpoxyAttribute lateinit var imageUrl: Uri

    override fun bind(holder: Holder) {
        holder.imageView.setImageURI(imageUrl)
        holder.titleView.text = title
    }
}

class Holder : KotlinHolder() {
    val titleView by bind<TextView>(R.id.title)
    val imageView by bind<ImageView>(R.id.image)
}

I took some time to write up kotlin samples in a new wiki page https://github.com/airbnb/epoxy/wiki/Kotlin-Model-Examples

I also played around with imagining a completely new kotlin pattern. I think this has potential, but could use some iteration, and possibly some changes to the processor to support it officially.

data class SampleKotlinModel(
    val title: String,
    val imageUrl: Uri
) : KotlinModel(R.layout.view_holder_page_header) {

    val titleView by bind<TextView>(R.id.title)
    val imageView by bind<ImageView>(R.id.image)

    override fun bind() {
        titleView.text = title
        imageView.setImageURI(imageUrl)
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create RecyclerView with multiple view types
Yes, it's possible. Just implement getItemViewType(), and take care of the viewType parameter in onCreateViewHolder() . So you do something like:
Read more >
Using the RecyclerView | CodePath Android Cliffnotes
In this case, we will define a Contact class which represents the data model being displayed by the RecyclerView: Java; Kotlin. public class...
Read more >
Create dynamic lists with RecyclerView - Android Developers
These two classes work together to define how your data is displayed. The ViewHolder is a wrapper around a View that contains the...
Read more >
Mixing Java and Kotlin in one project – tutorial
You can now consume the Java class from Kotlin or vice versa without any further actions. For example, adding the following Java class:...
Read more >
Writing Better Adapters - ProAndroidDev
Creating the view and the ViewHolder that holds the view information. ... kind of objects (this article uses Kotlin but it can be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found