Giter VIP home page Giter VIP logo

Comments (3)

vivchar avatar vivchar commented on June 18, 2024

It is the question for the stackoverflow. Please provide a link to the stackoverflow question and we will continue there.

What do you do after clicking at the remove button? Looks like you don't remove a child item in your list.

FYI: In the areContentsTheSame method you should use oldItem.equals(newItem) and you should override equals method in ParentModel and ChildItem. It is not critical in your case but is not correct in general. See Google Documentation about DiffUtil.

from rendererrecyclerviewadapter.

mengjiin avatar mengjiin commented on June 18, 2024

Thanks for your reply, I'm still trying to figure out how to post to stackoverflow, so please allow me to ask my question here for the time being.

I'd really appreciate if you could help me proceed, thank you!

=====================================================================

Here is my parent model:

public class ParentModel extends DefaultCompositeViewModel {
private String id;

public ParentModel(String id, List<ChildModel> items) {
    super(items);
    this.id = id;
}

public String getId() {
    return id;
}

@Override
public List<ChildModel> getItems() {
    return (List<ChildModel>) super.getItems();
}

public boolean equals(Object other) {
    if (other instanceof ParentModel) {
        ParentModel pm = (ParentModel) other;
        return pm.getId() == this.getId();
    }
    return false;
}

=====================================================================

Here is my child model:

public class ChildModel implements ViewModel {
private String id;
private String parent_id;

public ChildModel(String id, String parent_id) {
    this.id = id;
    this.parent_id = parent_id;
}

public String getId() { return id; }

public String getParentId() {
    return parent_id;
}

public boolean equals(Object other) {
    if (other instanceof ChildModel) {
        ChildModel cm = (ChildModel) other;
        return cm.getId() == this.getId();
    }
    return false;
}

}

=====================================================================

Here is my ParentDiffCallback (I changed areContentsTheSame based on your advice):

public class ParentDiffCallback extends DefaultDiffCallback {
@OverRide
public boolean areItemsTheSame(@nonnull final ParentModel oldItem, @nonnull final ParentModel newItem) {
return oldItem.getId().equals(newItem.getId());
}

@Override
public boolean areContentsTheSame(@NonNull final ParentModel oldItem, @NonNull final ParentModel newItem) {
	return oldItem.equals(newItem);
}

@Nullable
@Override
public Object getChangePayload(@NonNull final ParentModel oldItem, @NonNull final ParentModel newItem) {
	return null;
}

}

=====================================================================

Here is my child view renderer (I did remove child item with the click, please tell me if I did it wrong):
parentModels is the list of parent models:
List parentModels = new ArrayList();

private ViewRenderer childViewRenderer() {
ViewBinder.Binder childBinder = new ViewBinder.Binder() {
@OverRide
public void bindView(@nonnull ChildModel model, @nonnull ViewFinder finder, @nonnull List payloads) {
finder.setOnClickListener(R.id.ibDelete, new View.OnClickListener() {
@OverRide
public void onClick(View v) {
String parent_id = model.getParentId();
for (ParentModel pm : parentModels) {
if (pm.getId().equals(parent_id)) {
pm.getItems().remove(model);
}
}
parentAdapter.setItems(parentModels);
}
});
}
};

    return new ViewBinder<>(
            R.layout.child_item,
            ChildModel.class,
            childBinder
    );
}

=====================================================================

Here is my parent view renderer:

private ViewRenderer parentViewRenderer() {
return new CompositeViewBinder(
R.layout.parent_item,
R.id.rvChild,
ParentModel.class,
(model, finder, payloads) -> finder
.setOnClickListener(R.id.ibDelete, (v -> onLocationDeleteClicked(model)))
).registerRenderer(childViewRenderer());
}

from rendererrecyclerviewadapter.

vivchar avatar vivchar commented on June 18, 2024

Looks fine, need to debug and investigate, please use the stackoverflow. I have no time to debug your code. Thanks.

from rendererrecyclerviewadapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.