gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:38:21 25 4
gpt4 key购买 nike

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

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() 中(或者任何你更新更改或做某事的地方在适配器上):

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

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

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

关于android - 恢复 Activity 后,recyclerview 的项目变得不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com