Skip to content

Instantly share code, notes, and snippets.

@senneco
Created March 20, 2017 14:36
Show Gist options
  • Save senneco/ef2910a5b53aacdb053ebca21b10ef77 to your computer and use it in GitHub Desktop.
Save senneco/ef2910a5b53aacdb053ebca21b10ef77 to your computer and use it in GitHub Desktop.
class AccountHolder extends MvpViewHolder implements AccountView {
@InjectPresenter
AccountPresenter mAccountPresenter;
/**
@BindView(R.id.item_account_check_box)
CheckBox mCheckBox;
...
**/
private Account mAccount;
AccountHolder(View itemView) {
super(mParentDelegate, itemView);
}
@ProvidePresenter
AccountPresenter provideAccountPresenter() {
return new AccountPresenter(mAccount.getCode());
}
// Call from onBindViewHolder
public void setAccount(Account account) {
destroyMvpDelegate();
mAccount = account;
createMvpDelegate();
itemView.setOnClickListener(view -> mAccountsPresenter.onAccountClick(account));
}
// Critical! Return this item unique id
@Override
protected String getMvpChildId() {
return mAccount == null ? null : mAccount.getId();
}
// implement acccount veiw
}
/**
* Date: 15.11.2016
* Time: 9:41
*
* @author Savin Mikhail
*/
public abstract class MvpViewHolder extends RecyclerView.ViewHolder {
private MvpDelegate mMvpDelegate;
private final MvpDelegate mParentDelegate;
public MvpViewHolder(MvpDelegate<?> parentDelegate, final View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
mParentDelegate = parentDelegate;
}
@Nullable
protected MvpDelegate getMvpDelegate() {
if (getMvpChildId() == null) {
return null;
}
if (mMvpDelegate == null) {
mMvpDelegate = new MvpDelegate<>(this);
mMvpDelegate.setParentDelegate(mParentDelegate, getMvpChildId());
}
return mMvpDelegate;
}
protected void destroyMvpDelegate() {
if (getMvpDelegate() != null) {
getMvpDelegate().onSaveInstanceState();
getMvpDelegate().onDetach();
mMvpDelegate = null;
}
}
protected void createMvpDelegate() {
if (getMvpDelegate() != null) {
getMvpDelegate().onCreate();
getMvpDelegate().onAttach();
}
}
protected abstract String getMvpChildId();
}
@iandreyshev
Copy link

iandreyshev commented Feb 28, 2018

AccountHolder(View itemView) {
    super(mParentDelegate, itemView);
}

Откуда берётся поле mParentDelegate ?

@AlexeyKorshun
Copy link

AlexeyKorshun commented Jun 7, 2019

@senneco @xanderblinov этот пример некорректно работает, когда список внутри фрагмента, и открываем другой фрагмент. getMvpDelegate().onSaveInstanceState(); не вызывается и когда вернемся, то создастся новый презентер. На этот гист ссылка в Wiki есть, стоит поправить. я пока решил тем что сохраняю бандл после аттача.

@xanderblinov
Copy link

@AlexeyKorshun сделаешь gist как правильно? тогда его приаттачим

@AlexeyKorshun
Copy link

@xanderblinov поправил текущий немного вот ссылка

@askiiRobotics
Copy link

askiiRobotics commented Nov 17, 2019

AccountHolder(View itemView) {
    super(mParentDelegate, itemView);
}

Откуда берётся поле mParentDelegate ?

присоединяюсь к вопросу

@senneco
Copy link
Author

senneco commented Nov 17, 2019

Передается из родительской View. Например из MvpActivity или MvpFragment. У них есть специальный метод getMvpDelegate()

@askiiRobotics
Copy link

Передается из родительской View. Например из MvpActivity или MvpFragment. У них есть специальный метод getMvpDelegate()

Понял, спасибо!
Такой вопрос. createMvpDelegate не вызывается в текущем примере. Как происходит Attach?

@senneco
Copy link
Author

senneco commented Nov 18, 2019

Понял, спасибо!
Такой вопрос. createMvpDelegate не вызывается в текущем примере. Как происходит Attach?

Attach происходит примерно во-о-от здесь

@askiiRobotics
Copy link

Понял, спасибо!
Такой вопрос. createMvpDelegate не вызывается в текущем примере. Как происходит Attach?

Attach происходит примерно во-о-от здесь

Разобрался, получилось! Огромное спасибо.

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