恢复后 recyclerview 的项目变得不可见 Activity

Items of recyclerview gets invisble after resuming Activity

似乎没有其他人有这个问题,因为我没有找到任何答案。我的问题是 recyclerview 在第二次恢复 activity 后没有显示他的项目。要让项目恢复可见,我必须单击或滚动 recyclerview 上的任意位置。我真的不知道这些项目是不可见的,noteList 是空的还是有其他问题。也许这只是结构。我不知道。这是我的代码片段:
主要活动:

public class MainActivity extends AppCompatActivity implements RecyclerItemTouchHelper.RecyclerItemTouchHelperListener, NoteListAdapter.NotesAdapterListener {

private FirebaseAnalytics mFirebaseAnalytics;
private FirebaseAuth mFirebaseAuth;
public FirebaseUser mFirebaseUser;
private FirebaseFirestore db;

private RecyclerView recyclerView;
private RecyclerView.LayoutManager recyclerViewLayoutManager;
public NoteListAdapter adapter;
private List<Note> notesList;

private ListenerRegistration firestoreListener;
private RecyclerItemTouchHelper.RecyclerItemTouchHelperListener listener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getInstance().getCurrentUser();
    db = FirebaseFirestore.getInstance();

    recyclerView = findViewById(R.id.rvNoteList);
}

@Override
public void onStop() {
    super.onStop();
    adapter.stopListening();
}

@Override
protected void onResume() {
    super.onResume();

    notesList = new ArrayList<>();

    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(notesList, response, MainActivity.this, 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);

    firestoreListener = db.collection("users").document(firebase_user_uid).collection("notes")
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                    if (e != null) {
                        Log.e("LOL", "Listen failed!", e);
                        return;
                    }

                    for (DocumentSnapshot doc : documentSnapshots) {
                        Note note = doc.toObject(Note.class);
                        note.setId(doc.getId());
                        notesList.add(note);
                    }
                }
            });

    recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(getApplicationContext(), recyclerView, new RecyclerViewTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {
            if (notesList != null || notesList.size() <= 0) {
                Note note = notesList.get(position);
                updateNote(note);
            }
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

    adapter.startListening();
    setAlarms();

    invalidateOptionsMenu();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    View headerView = navigationView.getHeaderView(0);
    navUsername = (TextView) headerView.findViewById(R.id.displayname);
    navEmail = (TextView) headerView.findViewById(R.id.email);

    username  = sharedPreferences.getString("display_name", "");
    email = sharedPreferences.getString("email", "");

    if (username == null || email == null  || username.isEmpty() || email.isEmpty()) {
        email = mFirebaseUser.getEmail().toString();
        sharedPreferences.edit().putString("email", email).apply();
    }

    navUsername.setText(username);
    navEmail.setText(email);
}}  

尝试以下代码。将 recyclerView.setAdapter(adapter);(以及之后的 notifyDataSetChanged())放在 runOnUiThread() 中(或者在 Adapter 上更新更改或执行某事的任何地方):

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //Do something on UiThread

        recyclerView.setAdapter(adapter);
        notifyDataSetChanged();
    }
});

因为它改变了 UI,所以我可以用 runOnUiThread() 方法显示它。