简体   繁体   English

环氧树脂:如何使用@AutoModel批注并在没有控制器的情况下处理点击?

[英]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. 我开始使用环氧树脂库,因为我正在寻找一个使用RecyclerView组件时编写更少代码的库。

The EpoxyRecyclerView seems to be a great component since you can simply give the models (basically a simple list), thanks to the setModels method. EpoxyRecyclerView似乎是一个很棒的组件,因为有了setModels方法,您可以简单地提供模型(基本上是一个简单的列表)。

Question 1 问题1

The first question is : it is possible to automatically set an id to a model (using the @AutoModel ?) without using a controller ? 第一个问题是:可以在不使用控制器的情况下自动为模型设置ID(使用@AutoModel吗?)。

For example, I have the following DataBindingEpoxyModel class : 例如,我具有以下DataBindingEpoxyModel类:

@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 : 我在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. 这段代码崩溃是因为我没有为HeaderModel_()实例设置id 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 ? AutoModel批注只能在字段上使用,因此有没有办法在没有控制器的情况下自动为我的模型实例设置ID?

Question 2 问题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 ) ? 使用问题1的代码,如何处理对布局的几个小部件的单击(在我的情况下,单击TextView或单击itemView )? Is there a way to override the holder used by the DataBindingEpoxyModel in order to handle the click directly into the holder ? 有没有一种方法可以覆盖DataBindingEpoxyModel所使用的支架,以便直接将点击处理到支架中?

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). 在我来说,我不想定义OnClickListener作为我的属性HeaderModel ,因为我想定义类型的所有型号相同的行为HeaderModel (不使用控制器)。

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 . 编辑:我找到了问题2的答案。我只需要重写DataBindingEpoxyModelbind方法之一。 But I do not find a way to automatically set an id to a model without a controller and the @AutoModel annotation. 但是我没有找到一种无需控制器和@AutoModel注释即可自动为模型设置ID的@AutoModel

Here the answer from the github repo for the first question : 这是来自github回购的第一个问题的答案:

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. 无法自动设置ID(除了自动模型,无论如何我最近都不建议这样做)-您应该始终在每个模型上手动设置ID。 It is best to do this by setting a human readable string. 最好通过设置人类可读的字符串来做到这一点。 This is detailed more in the wiki Wiki中对此有更详细的说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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