Я пытаюсь сохранить положение элементов приложения списка задач после вертикального перетаскивания элементов списка задач.Я использую RecyclerView / FirestoreRecylerAdapter / ItemTouchHelper / Firestore.В onMove () - метод ItemTouchHelper.Callback я получаю доступ к Firestore, я думаю, с асинхронными вызовами.Это приводит к нежелательным изменениям содержимого в моем RecyclerView.Представьте, что у меня есть список из 4 предметов с названиями A, B, C, D. Теперь я перетаскиваю Предмет с заголовком A из позиции 0 (вверху) в позицию 1. Как и у этого элемента, название B также меняется на A. Но этотолько на пользовательском интерфейсе, а не в БД.Когда я переключаюсь на другое занятие и возвращаюсь, названия снова верны.Я предполагаю, что это связано с асинхронными вызовами.
//part of MainActivity onCreate()
mFirestore = FirebaseFirestore.getInstance();
FirebaseFirestore.setLoggingEnabled(true);
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(true)
.build();
mFirestore.setFirestoreSettings(settings);
mRecyclerView = findViewById(R.id.recycler_view);
mUser = FirebaseAuth.getInstance().getCurrentUser();
if(mUser == null){
startSignIn();
}else{
Query mQuery = mFirestore.collection("todos").whereEqualTo("author", mUser.getUid()).whereEqualTo("done", false).orderBy("position", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Todo> options = new FirestoreRecyclerOptions.Builder<Todo>()
.setQuery(mQuery, Todo.class)
.build();
mAdapter = new TodoAdapter(options);
initRecyclerView();
//ItemTouchHelper.Callback method as part of initRecyclerView()
@Override
public boolean onMove(final RecyclerView recyclerView, final RecyclerView.ViewHolder source, final RecyclerView.ViewHolder target) {
if (source.getItemViewType() != target.getItemViewType()) {
return false;
}
int fromPos = source.getAdapterPosition();
int toPos = target.getAdapterPosition();
//Change items position when an item was draged & droped vertically downwards
if(toPos > fromPos) {
DocumentReference docTo = mAdapter.getSnapshots().getSnapshot(fromPos).getReference();
docTo.update("position", toPos);
int current = fromPos+1;
while(current <= toPos){
DocumentReference docCurrent = mAdapter.getSnapshots().getSnapshot(current).getReference();
docCurrent.update("position", current-1);
current ++;
}
//Change items position when an item was draged & droped vertically upwards
}else if (toPos < fromPos){
//TODO implement
} else {
return false;
}
mAdapter.notifyItemMoved(fromPos, toPos);
return true;
}
//adapter class
public class TodoAdapter extends FirestoreRecyclerAdapter<Todo, TodoAdapter.TodoHolder> {
private static final String TAG = "DOIT_DEBUG: ";
private OnItemClickListener listener;
private View viewItem;
public TodoAdapter(@NonNull FirestoreRecyclerOptions<Todo> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull TodoHolder holder, int position, @NonNull Todo model) {
holder.textViewTitle.setText(model.getTitle());
holder.textViewDescription.setText(model.getDescription());
}
@NonNull
@Override
public TodoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
viewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_todo,
parent, false);
return new TodoHolder(viewItem);
}
public void deleteItem(final int position) {
getSnapshots().getSnapshot(position).getReference().delete();
}
public void setDoneItem(int position){
getSnapshots().getSnapshot(position).getReference().update("done", true);
viewItem.setEnabled(false);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
public interface OnItemClickListener{
void onItemClick (DocumentSnapshot documentSnapshot, int position);
}
public class TodoHolder extends RecyclerView.ViewHolder {
private TextView textViewTitle;
private TextView textViewDescription;
private String documentID;
public TodoHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.todo_title);
textViewDescription = itemView.findViewById(R.id.todo_description);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION && listener != null){
listener.onItemClick(getSnapshots().getSnapshot(position), position);
}
}
});
}
}
//model class
public class Todo {
private String title;
private String description;
private String author;
private boolean done;
private int position;
private Date created;
public Todo(){} // Needed for Firebase
public Todo(String title, String description, String author, boolean done, int position, Date created) {
this.title = title;
this.description = description;
this.author = author;
this.done = done;
this.position = position;
this.created = created;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getAuthor() {
return author;
}
public boolean getDone() {
return done;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setAuthor(String author) {
this.author = author;
}
public void setDone(boolean done) {
this.done = done;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
@ServerTimestamp
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
// структура БД
Коллекция "todos" Поля документа: строка автора, созданная метка времени, строка описания, логическое значение done, номер позиции, строка заголовка
Я ожидаю, что изменение позиции перетаскивания будет сохраняться в Firestore и что RecyclerView отображает только правильное перемещение элементов и никаких других изменений.