所以,让我们从头开始吧。我有一个与实时Firestore
数据库一起使用的项目。我是用户EventListener
添加,更新和删除RecyclerView
中的项目。代码如下所示:
片段
public class ActionsFragment extends Fragment implements ActionsFragmentBase {
private Unbinder unbinder;
private ActionsPresenter presenter;
private ActionsRecyclerAdapter actionsAdapter;
private int state;
private int type;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
presenter = new ActionsPresenter(this);
View v = inflater.inflate(R.layout.fragment_actions, container, false);
type = this.getArguments().getInt("type");
setHasOptionsMenu(true);
unbinder = ButterKnife.bind(this, v);
return v;
}
@Override
public void onResume() {
super.onResume();
presenter.notifyFragmentStarted(type);
}
@Override
public void onPause() {
super.onPause();
presenter.removeRegistration();
}
@Override
public void onStop() {
super.onStop();
presenter.removeRegistration();
}
...
public void handleListUpdate(DocumentChange.Type type, int newIndex, int oldIndex, Action action) {
if (actionsAdapter != null) {
switch (type) {
case ADDED:
actionsAdapter.addItem(newIndex, action);
break;
case MODIFIED:
actionsAdapter.updateItem(oldIndex, newIndex, action);
break;
case REMOVED:
actionsAdapter.removeItem(oldIndex);
break;
}
}
}
public boolean isListEmpty() {
return actionsAdapter == null || actionsAdapter.getItemCount() == 0;
}
public boolean isAdapterExists() {
return actionsAdapter != null;
}
@Override
public void onDestroyView() {
super.onDestroyView();
actionsAdapter = null;
unbinder.unbind();
presenter.destroy();
}
}
演示
public class ActionsPresenter {
private ActionsFragmentBase fragment;
private ActionsModel model;
private Query actionsQuery;
private ListenerRegistration actionsRegistration;
public ActionsPresenter(ActionsFragmentBase fragment){
this.fragment = fragment;
this.model = new ActionsModel();
}
public void notifyFragmentStarted(int type) {
switch (type) {
/* Getting Query depending on type */
}
setRegistration(type);
}
private void notifyViewCreated(int state) {
fragment.showRequiredViews();
switch (state) {
/* Setting up Views depending on state */
}
}
@SuppressWarnings("unchecked")
public void setRegistration(int type) {
actionsRegistration = actionsQuery.addSnapshotListener((documentSnapshots, e) -> {
if (e == null) {
for (DocumentChange dc : documentSnapshots.getDocumentChanges()) {
DocumentSnapshot doc = dc.getDocument();
Action action;
/* Getting Action object depending on type */
if (fragment != null) {
if (!fragment.isAdapterExists() && fragment.isListEmpty()) {
fragment.setupLayouts(true, true);
notifyViewCreated(Globals.FragmentState.STATE_CONTENT);
}
fragment.handleListUpdate(dc.getType(), dc.getNewIndex(), dc.getOldIndex(), action);
}
}
if (fragment != null && fragment.isListEmpty()) {
fragment.setupLayouts(true, false);
notifyViewCreated(Globals.FragmentState.STATE_NO_DATA);
}
} else {
if (fragment != null) {
fragment.setupLayouts(false, false);
notifyViewCreated(Globals.FragmentState.STATE_NO_INTERNET_CONNECTION);
}
}
});
}
...
public void removeRegistration() {
actionsRegistration.remove();
}
public void destroy() {
removeRegistration();
fragment = null;
model = null;
}
}
问题是我的RecyclerView
项目在片段恢复上重复。我不知道如何恢复EventListener
而不是在RecyclerView
简历上重新创建整个Fragment
。提前谢谢。
UPD
我刚刚检查了官方样本,发现每次Activity
或Fragment
停止时都会清除RecyclerView
个项目。当然它有效,但我真的不想在片段恢复上重新加载整个列表。但它可以像临时解决方法一样使用。