我有多个片段做同样的事情,但以不同的方式呈现给用户。但是,所有这些片段中的功能更像是删除,添加等等。也就是说,我不想复制代码。因此创建了一个管理器类,以便我可以拥有集中代码。但是,现在的问题是,当用户执行动作时,如果用户正在删除项目,则片段不会刷新。所以,我需要将消息发送到manager类的片段以刷新列表。我有以下伪代码来提供见解......
public class MyFragment extends Fragment{
ManagerClass mManagerClass;
private void onItemSelected() {
mManagerClass = new ManagerClass(itemId);
}
public void refreshItems() {
ItemDao.query();
}
}
public class ManagerClass {
public ManagerClass(int itemId) {
DeleteItem(itemId);
}
private void DeleteItem(int itemId) {
//when this task completes it should call the MrFragment.refreshItems();
//Keep in mind that I cannot pass the Fragment becuse this ManagerClass is designed to handle more than two fragments
//MAY BE I SHOULD DO CALLBACK BUT HOW?.... When i try to implement there several callbacks but not sure which one should I use and how...
}
}
Any help would be greatly appreciated...
答案 0 :(得分:0)
我选择了第二个选项,Observer模式,就像一个魅力!