所以我有我的MainActivity
其中有一个BottomNavigationView
,其中有 3 个不同的选项卡,当我单击它们时,它们会将我重定向到 3 个不同的片段。
In FragmentA
我有一个RecyclerView
对于项目,每个项目都有一个按钮。
当我单击所述按钮时,我想将该对象发送到FragmentB
所以我可以将它添加到ArrayList<CustomObject>
并更新RecyclerView
in FragmentB
显示该项目。
唯一的问题是我不知道如何在单击按钮时发送该对象。
adapter.setOnItemRemoveListener(new RemoveItemAdapter.OnItemRemoveListener() {
@Override
public void onItemRemove(int position) {
//Do I send it from here?
}
});
首先要实现的是可分包在你的 Model(Object) 类中,然后从你的 Fragment A 中调用这个 -
Fragment fragmentA = new FragmentGet();
Bundle bundle = new Bundle();
bundle.putParcelable("CustomObject", customObject);
fragmentA .setArguments(bundle);
另外,在片段 B 中,您还需要获取参数 -
Bundle bundle = getActivity().getArguments();
if (bundle != null) {
model = bundle.getParcelable("CustomObject");
}
您的自定义对象类将如下所示 -
public class CustomObject implements Parcelable {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.description);
}
public CustomObject() {
}
protected CustomObject(Parcel in) {
this.name = in.readString();
this.description = in.readString();
}
public static final Parcelable.Creator<CustomObject> CREATOR = new Parcelable.Creator<CustomObject>() {
@Override
public CustomObject createFromParcel(Parcel source) {
return new CustomObject(source);
}
@Override
public CustomObject[] newArray(int size) {
return new CustomObject[size];
}
};
}
只需从回收器视图项单击侦听器中调用 Fragment B 并使用上述代码通过 Parcelable 传递自定义对象即可。
希望能帮助到你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)