简体   繁体   中英

Epoxy : how to use the @AutoModel annotation and handle clicks without controller?

I am starting to use the epoxy library because I am looking for a library to write less code while using the RecyclerView component.

The EpoxyRecyclerView seems to be a great component since you can simply give the models (basically a simple list), thanks to the setModels method.

Question 1

The first question is : it is possible to automatically set an id to a model (using the @AutoModel ?) without using a controller ?

For example, I have the following DataBindingEpoxyModel class :

@EpoxyModelClass(layout = R.layout.item_header)
abstract class HeaderModel
  : DataBindingEpoxyModel()
{

  @StringRes
  @EpoxyAttribute
  var title: Int? = null

}

And I use it like this in my Fragment :

val models = mutableListOf<EpoxyModel<*>>()
models.add(HeaderModel_().title(R.string.catalogue_header_categories_title)
//  [...]
 recyclerView?.setModels(models)

This code crashes because I do not set an id to the HeaderModel_() instance. The AutoModel annotation have to be used on a field only, so is there a way to automatically set an id to my model instance with no controller ?

Question 2

The second question is : it is possible to handle a click without using a controller ?

Using the code of the question 1, how to handle a click on several widget of the layout (in my case, a click on the TextView or a click on the itemView ) ? Is there a way to override the holder used by the DataBindingEpoxyModel in order to handle the click directly into the holder ?

In my case I do not want to define the OnClickListener as an attribute of my HeaderModel because I would like to define the same behavior for all models of the type HeaderModel (without using a controller).

Thank you in advance for you help !

Edit : I found the answer of the question 2. I just need to override one of the bind method of the DataBindingEpoxyModel . But I do not find a way to automatically set an id to a model without a controller and the @AutoModel annotation.

Here the answer from the github repo for the first question :

There is no way to automatically set an id (besides automodel, which I wouldn't recommend these days anyway) - you should always set an id manually on each model. It is best to do this by setting a human readable string. This is detailed more in the wiki

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM