Option for manually binding custom variables #19

Closed
jamolkhon opened this Issue Mar 30, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@jamolkhon

jamolkhon commented Mar 30, 2017

I'd like to able to bind presenter or other non-static handlers for views inside items:

  <variable
      name="presenter"
      type="com.example.module.somemodule.SomePresenter"/>

  <variable
      name="item"
      type="com.example.viewmodels.SomeViewModel"/>
</data>
...
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    onClick="@{presenter.doSmth(item)}"/>

Getting hold of a view holder would allow such thing. But I can only access it in Type<ItemBinding>.onClick():

Type<SomeBinding> someType = new Type<>(R.layout.some_item);
someType.onClick(new Function1<ViewHolder<SomeBinding>, Unit>() {
  @Override
  public Unit invoke(ViewHolder<SomeItemBinding> viewHolder) {
    viewHolder.getBinding().setVariable(BR.presenter, presenter); // I need this somewhere outside
    return Unit.INSTANCE;
  }
});
@nitrico

This comment has been minimized.

Show comment
Hide comment
@nitrico

nitrico Mar 30, 2017

Owner

Hi @jamolkhon

I tried to add a better support for this but it is not very clear to me where to get the new items from (the presenter in this case)... use always the same item? provide an additional collection? None of them seems to be really useful. I think there will always be only one item that really represents that element in the list, so I'm not sure if I should change that.

Anyway, you should be able to do what you want in the current version of the library in the onBind method. The viewholder is now provided to every callback: onBind/onRecycle/onClick/onLongClick so I recommend you doing so in onBind and to use the proper method in binding for that (instead of the generic setVariable). When you declare one variable in the layout (presenter, for example), then you have a set method in the binding to set this variable (setPresenter(presenter), in this case. After rebuilding the project):

@Override
public void onBind(@NotNull ViewHolder<SomeBinding> holder) {
    holder.getBinding().setPresenter(presenter)
}

Right now I'm working on adding a new callback for onCreate that will be much more appropriate to do so if you are using the same presenter for all the items, as well as for setting click listeners. This feature should be published in a new version somewhen soon.

Owner

nitrico commented Mar 30, 2017

Hi @jamolkhon

I tried to add a better support for this but it is not very clear to me where to get the new items from (the presenter in this case)... use always the same item? provide an additional collection? None of them seems to be really useful. I think there will always be only one item that really represents that element in the list, so I'm not sure if I should change that.

Anyway, you should be able to do what you want in the current version of the library in the onBind method. The viewholder is now provided to every callback: onBind/onRecycle/onClick/onLongClick so I recommend you doing so in onBind and to use the proper method in binding for that (instead of the generic setVariable). When you declare one variable in the layout (presenter, for example), then you have a set method in the binding to set this variable (setPresenter(presenter), in this case. After rebuilding the project):

@Override
public void onBind(@NotNull ViewHolder<SomeBinding> holder) {
    holder.getBinding().setPresenter(presenter)
}

Right now I'm working on adding a new callback for onCreate that will be much more appropriate to do so if you are using the same presenter for all the items, as well as for setting click listeners. This feature should be published in a new version somewhen soon.

@nitrico nitrico closed this Mar 31, 2017

@nitrico

This comment has been minimized.

Show comment
Hide comment
@nitrico

nitrico Apr 1, 2017

Owner

Added onCreate callback in version 2.2.0. Please, check also changelog as there are some other breaking changes.

Owner

nitrico commented Apr 1, 2017

Added onCreate callback in version 2.2.0. Please, check also changelog as there are some other breaking changes.

@jamolkhon

This comment has been minimized.

Show comment
Hide comment
@jamolkhon

jamolkhon Apr 2, 2017

Great job! Thank you!

Great job! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment