似乎没有其他人遇到这个问题,因为我没有找到任何答案。我的问题是,recyclerview在第二次恢复活动后没有显示他的物品。为了使这些物品重新可见,我必须在recyclerview上单击或滚动任何位置。我真的不知道这些项目是否不可见,noteList是否为空或是否存在其他问题。也许仅仅是结构。我不确定。这是我的代码段:
MainActivity:
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);
}}
答案 0 :(得分:1)
尝试以下代码。将recyclerView.setAdapter(adapter);
(和之后的notifyDataSetChanged()
)放在runOnUiThread()
内(或您在Adapter
上进行更新或更改的地方)
runOnUiThread(new Runnable() {
@Override
public void run() {
//Do something on UiThread
recyclerView.setAdapter(adapter);
notifyDataSetChanged();
}
});
因为它可以改变用户界面,所以我可以使用runOnUiThread()
方法来显示它。