我有一个Note-App。在主要活动中,注释将以RecyclerView
列出。要在数据库中应用更改,我使用FirestoreRecyclerOptions
。
但是我有一个大问题。首先,一切正常。当ListItems更改一次或多次时,一切都会变得混乱。
然后这些事情发生了:
在“视频”中,您可以在滚动到“回收者”视图中后看到所有项目将正常工作。不知道为什么也许recyclerview比真正被重新加载?
这是我设置RecyclerView的方法以及调用适配器(MainActivity的片段)的方式:
public class MainActivity extends AppCompatActivity implements RecyclerItemTouchHelper.RecyclerItemTouchHelperListener {
private FirebaseFirestore db;
public NoteListAdapter adapter;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager recyclerViewLayoutManager;
private ListenerRegistration firestoreListener;
private RecyclerItemTouchHelper.RecyclerItemTouchHelperListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rvNoteList);
db = FirebaseFirestore.getInstance();
setUpRecyclerView();
}
private void setUpRecyclerView() {
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(true).build();
db.setFirestoreSettings(settings);
Query query = db.collection("users").document(firebase_user_uid).collection("notes");
FirestoreRecyclerOptions<Note> response = new FirestoreRecyclerOptions.Builder<Note>().setQuery(query, Note.class).build();
adapter = new NoteListAdapter(response, MainActivity.this);
recyclerViewLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(recyclerViewLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new CustomRecyclerViewDivider(this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(adapter);
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, MainActivity.this);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
adapter.checkIflayoutMustImprove();
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
adapter.removeItem(viewHolder.getAdapterPosition());
}
private void deleteNote(String id) {
db.collection("users").document(firebase_user_uid).collection("notes")
.document(id)
.delete()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mFirebaseAnalytics.logEvent(NOTE_DELETED_EVENT, null);
}
});
adapter.notifyDataSetChanged();
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}}
这是ItemRecyclerTouchHelper:
public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
private RecyclerItemTouchHelperListener listener;
public RecyclerItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
super(dragDirs, swipeDirs);
this.listener = listener;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return true;
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder != null) {
final View foregroundView = ((NoteViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().onSelected(foregroundView);
}
}
@Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((NoteViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final View foregroundView = ((NoteViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().clearView(foregroundView);
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((NoteViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
}
@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
return super.convertToAbsoluteDirection(flags, layoutDirection);
}
public interface RecyclerItemTouchHelperListener {
void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
}}
我在这个问题上待了两个星期,我真的需要帮助。我希望有人能帮助我。我什至不知道是什么问题。问题是RecyclerView或Firebase还是一个简单的错误?如果您需要其他文件,请告诉我。 感谢您的帮助。
编辑NoteListAdapter
公共类NoteListAdapter扩展了FirestoreRecyclerAdapter {
private Context context;
private RelativeLayout no_data_layout;
private Animation fadeIn, fadeOut;
public NoteListAdapter(@NonNull FirestoreRecyclerOptions<Note> options, @NonNull Context context) {
super(options);
this.context = context;
}
@Override
protected void onBindViewHolder(@NonNull NoteViewHolder holder, int position, @NonNull Note note) {
holder.bind(context, note, getSnapshots().getSnapshot(position).getId(), note.getNote_image_url());
checkIflayoutMustImprove();
//setAlarms
if (note.getRemindeDate() != null) {
if (note.getRemindeDate().before(new Date())) {
note.setRemindeDate(null);
}
if (note.getRemindeDate() != null) {
Intent i = new Intent(context, TodoNotificationService.class);
i.putExtra(TodoNotificationService.TODOUUID, note.getId());
i.putExtra(TodoNotificationService.TODOTEXT, note.getTitle());
i.putExtra(TodoNotificationService.TODOCONTENT, note.getContent());
MainActivity.createAlarm(context, i, getSnapshots().getSnapshot(position).getId().hashCode(), note.getRemindeDate().getTime());
}
}
if (note.getRemindeDate() == null) {
Intent i = new Intent(context, TodoNotificationService.class);
int id = getSnapshots().getSnapshot(position).getId().hashCode();
MainActivity.deleteAlarm(context, i, id);
}
}
@Override
public int getItemCount() {
return getSnapshots().size();
}
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note_view, parent, false);
return new NoteViewHolder(view);
}
public void removeItem(int position) {
getSnapshots().getSnapshot(position).getReference().delete();
notifyItemRemoved(position);
checkIflayoutMustImprove();
}}
答案 0 :(得分:0)
放入此行
adapter.notifyDataSetChanged();
在您的
内部addOnCompleteListener()
所以您的onCompleteListener看起来像这样
addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mFirebaseAnalytics.logEvent(NOTE_DELETED_EVENT, null);
adapter.notifyDataSetChanged();
}
});
如果这不起作用,那么您还应该发布适配器类。
答案 1 :(得分:0)
因此,感谢您的所有帮助,但没人能回答。我在我的应用程序中编码了一个immersiveMode()。即使有任何交互,他也将屏幕设置为全屏。我注意到我删除了immersiveMode()方法后,recyclerview遇到的所有错误都消失了。我不能说为什么,但是现在可以了。但是,谢谢大家。 :)