我正在根据从服务器获取的JSON响应来呈现一个表单。
我的用例包括从单选按钮中听到一次单击,根据radioButton选择切换特定文本字段的可见性,并使用可视textView刷新布局。
预期的输出应该是用现在可见的textView更新相同的视图,但是我现在看到相同的表单两次,第一次是默认状态,第二次是更新状态。
我是否以某种方式创建了一个全新的model_类并将其传递给控制器?我只想更改现有模型的布尔字段并更新视图。
我的模型课
@EpoxyModelClass(layout = R.layout.layout_panel_input)
abstract class PanelInputModel(
@EpoxyAttribute var panelInput: PanelInput,
@EpoxyAttribute var isVisible: Boolean,
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash) var context: Context,
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash) var textChangedListener: InputTextChangedListener,
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash) var radioButtonSelectedListener: RadioButtonSelectedListener,
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash) var validationChangedListener: ValidationChangedListener
) : EpoxyModelWithHolder<PanelInputModel.PanelInputHolder>() {
@EpoxyAttribute var imageList = mutableListOf<ImageInput>()
override fun bind(holder: PanelInputHolder) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
generateViews(holder, inflater, panelInput.elements) // Generates textViews, radioButtons, etc, based on ElementType enum inside Panel input
}
fun generateRadioButtonView(element: Element) {
// Created a custom listener and calling its function
radioButtonSelectedListener.radioButtonSelected(chip.id, chip.text.toString())
}
fun generateTextView() {
// Show/hide textView based on isVisible value
}我的控制器班
class FormInputController(
var context: Context,
var position: Int, // Fragment Position in PagerAdapter
var textChangedListener: InputTextChangedListener,
var radioButtonSelectedListener: RadioButtonSelectedListener,
var validationChangedListener: ValidationChangedListener
) : TypedEpoxyController<FormInput>() {
override fun buildModels(data: FormInput?) {
val panelInputModel = PanelInputModel_(
data as PanelInput,
data.isVisible,
context,
textChangedListener,
radioButtonSelectedListener,
validationChangedListener
)
panelInputModel.id(position)
panelInputModel.addTo(this)
}
}我的片段实现了on单选按钮选中的侦听器,修改了formInput.isVisible = true并调用了formInputController.setData(componentList)
请帮帮我,谢谢!
发布于 2021-02-11 16:34:47
我不认为你使用Epoxy是正确的,这不是应该的。
R.layout.layout_panel_input指定的布局文件中膨胀的,因此根本不需要膨胀.。
然后用这样的方式创建你的持有者:
class PanelInputHolder : KotlinHolder() {
val textView by bind<TextView>(R.id.your_text_view_id)
val button by bind<Button>(R.id.your_button_id)
}就像这样:
@EpoxyModelClass
class PanelInputModel : EpoxyModelWithHolder<PanelInputHolder>() {
@EpoxyAttribute
lateinit var text: String
@EpoxyAttribute(DoNotHash)
lateinit var listener: View.OnClickListener
override fun getDefaultLayout(): Int {
return R.layout.layout_panel_input
}
override fun bind(holder: PanelInputHolder) {
// here set your views
holder.textView.text = text
holder.textView.setOnClickListener(listener)
}
override fun unbind(holder: PanelInputHolder) {
// here unset your views
holder.textView.text = null
holder.textView.setOnClickListener(null)
}
}class FormInputController : TypedEpoxyController<FormInput>() {
override fun buildModels(data: FormInput?) {
data?.let {
// do your layout as you want, with the models you specify
// for example a header
PanelInputModel_()
.id(it.id)
.text("Hello WOrld!")
.listener { // do something here }
.addTo(this)
// generate a model per item
it.images.forEach {
ImageModel_()
.id(it.imageId)
.image(it)
.addTo(this)
}
}
}
}在选择您的id时,请记住Epoxy将跟踪这些信息,并在有吸引力的情况下进行更新,因此不要使用位置,而是使用不会被复制的唯一id。
https://stackoverflow.com/questions/65230968
复制相似问题