英文:
Rebuilding the recycler view causes it to disappear
问题
My Fragments OnViewCreated code
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
search_keyword = getView().findViewById(R.id.exp_search);
search_btn = getView().findViewById(R.id.exp_search_btn);
ccp = getView().findViewById(R.id.exp_ccp);
refresh = getView().findViewById(R.id.refresh);
country = ccp.getSelectedCountryName();
setUpRecyclerView(country);
Log.e("explore frag","1");
db.collection("Users").document(id).collection("preferences").document("event").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
String anime = documentSnapshot.get("anime").toString();
String art = documentSnapshot.get("art").toString();
String edu = documentSnapshot.get("edu").toString();
String gaming = documentSnapshot.get("gaming").toString();
String music = documentSnapshot.get("music").toString();
String other = documentSnapshot.get("other").toString();
String all = documentSnapshot.get("all").toString();
String search_key = search_keyword.getText().toString().trim();
country = ccp.getSelectedCountryName();
if (all.equals("1") && TextUtils.isEmpty(search_key)) {
setUpRecyclerView(country);
Log.e("explore frag","2");
} else if (all.equals("1") && !(TextUtils.isEmpty(search_key))) {
setUpRecyclerViewWithKeyword(country, search_key);
Log.e("explore frag","3");
} else if (all.equals("0") && TextUtils.isEmpty(search_key)) {
setUpRecyclerViewWithPreferences(country, anime, art, edu, gaming, music, other);
Log.e("explore frag","4");
} else {
setUpRecyclerViewWithKeywordAndPreferences(country, search_key, anime, art, edu, gaming, music, other);
Log.e("explore frag","5");
}
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
adapter.setOnItemClickListener(
new ExplorePageAdapter.OnItemClickListener() {
@Override
public void OnItemClick(DocumentSnapshot documentSnapshot, int position) {
String id = documentSnapshot.getId();
Intent intent = new Intent(getActivity(), event_customer_view_activity.class);
intent.putExtra("Event_ID", id);
startActivity(intent);
}
}
);
}
RecylerView building method
private void setUpRecyclerView(String country) {
Log.e("explore frag", country);
Query query = Cref.whereEqualTo("country", country);
FirestoreRecyclerOptions<POJO_explore_page_item> options = new
FirestoreRecyclerOptions.Builder<POJO_explore_page_item>()
.setQuery(query, POJO_explore_page_item.class)
.build();
adapter = new ExplorePageAdapter(options);
RecyclerView recyclerView = getView().findViewById(R.id.explore_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
Attempted solutions:
I have tried adding recyclerView.setAdapter(null);
and recyclerView.setLayoutManager(null);
before
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
I have also tried rebuilding the recycler view in my OnViewCreated method
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
search_keyword = getView().findViewById(R.id.exp_search);
search_btn = getView().findViewById(R.id.exp_search_btn);
ccp = getView().findViewById(R.id.exp_ccp);
refresh = getView().findViewById(R.id.refresh);
country = ccp.getSelectedCountryName();
Log.e("explore frag",country);
Query query = Cref.whereEqualTo("country", country);
FirestoreRecyclerOptions<POJO_explore_page_item> options = new
FirestoreRecyclerOptions.Builder<POJO_explore_page_item>()
.setQuery(query, POJO_explore_page_item.class)
.build();
adapter = new ExplorePageAdapter(options);
final RecyclerView recyclerView = getView().findViewById(R.id.explore_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
Log.e("explore frag","1");
// ... (remaining code)
}
Neither of these two attempts worked. Any suggestions?
英文:
In my code, I am building a recycler view, via the methodsetUpRecyclerView(country);
, when my fragment is fast created and later on I call the same method (or various other methods that build a recycler view e.g.setUpRecyclerViewWithKeyword(country, search_key);
, setUpRecyclerViewWithPreferences(country, anime, art, edu, gaming, music, other);
, setUpRecyclerViewWithKeywordAndPreferences(country, search_key, anime, art, edu, gaming, music, other);
) to build the recyler view again. This is illustrated below:
My Fragments OnViewCreated code
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
search_keyword = getView().findViewById(R.id.exp_search);
search_btn = getView().findViewById(R.id.exp_search_btn);
ccp = getView().findViewById(R.id.exp_ccp);
refresh = getView().findViewById(R.id.refresh);
country = ccp.getSelectedCountryName();
setUpRecyclerView(country);
Log.e("explore frag","1");
db.collection("Users").document(id).collection("preferences").document("event").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
String anime = documentSnapshot.get("anime").toString();
String art = documentSnapshot.get("art").toString();
String edu = documentSnapshot.get("edu").toString();
String gaming = documentSnapshot.get("gaming").toString();
String music = documentSnapshot.get("music").toString();
String other = documentSnapshot.get("other").toString();
String all = documentSnapshot.get("all").toString();
String search_key = search_keyword.getText().toString().trim();
country = ccp.getSelectedCountryName();
if (all.equals("1") && TextUtils.isEmpty(search_key)) {
setUpRecyclerView(country);
Log.e("explore frag","2");
} else if (all.equals("1") && !(TextUtils.isEmpty(search_key))) {
setUpRecyclerViewWithKeyword(country, search_key);
Log.e("explore frag","3");
} else if (all.equals("0") && TextUtils.isEmpty(search_key)) {
setUpRecyclerViewWithPreferences(country, anime, art, edu, gaming, music, other);
Log.e("explore frag","4");
} else {
setUpRecyclerViewWithKeywordAndPreferences(country, search_key, anime, art, edu, gaming, music, other);
Log.e("explore frag","5");
}
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
adapter.setOnItemClickListener(
new ExplorePageAdapter.OnItemClickListener() {
@Override
public void OnItemClick(DocumentSnapshot documentSnapshot, int position) {
String id = documentSnapshot.getId();
Intent intent = new Intent(getActivity(),
event_customer_view_activity.class);
intent.putExtra("Event_ID", id);
startActivity(intent);
// getActivity().finish();
}
}
);
}
RecylerView building method
private void setUpRecyclerView(String country) {
Log.e("explore frag",country);
Query query = Cref.whereEqualTo("country", country);
FirestoreRecyclerOptions<POJO_explore_page_item> options = new
FirestoreRecyclerOptions.Builder<POJO_explore_page_item>()
.setQuery(query, POJO_explore_page_item.class).
build();
adapter = new ExplorePageAdapter(options);
RecyclerView recyclerView = getView().findViewById(R.id.explore_recycler);
// recyclerView.setAdapter(null);
// recyclerView.setLayoutManager(null);
// recyclerView.getRecycledViewPool().clear();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
My problem is that whenever I try to rebuild the new recycler view, there is no result unllke the first time the recyler view is built. It is almost as though the recycler view disappeares.
Attempted solutions:
I have tried adding recyclerView.setAdapter(null);
andrecyclerView.setLayoutManager(null);
before
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
I have also tried rebuilding the recyler view in my OnViewCreated method
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
search_keyword = getView().findViewById(R.id.exp_search);
search_btn = getView().findViewById(R.id.exp_search_btn);
ccp = getView().findViewById(R.id.exp_ccp);
refresh = getView().findViewById(R.id.refresh);
country = ccp.getSelectedCountryName();
//setUpRecyclerView(country);
///
Log.e("explore frag",country);
Query query = Cref.whereEqualTo("country", country);
FirestoreRecyclerOptions<POJO_explore_page_item> options = new
FirestoreRecyclerOptions.Builder<POJO_explore_page_item>()
.setQuery(query, POJO_explore_page_item.class).
build();
adapter = new ExplorePageAdapter(options);
final RecyclerView recyclerView = getView().findViewById(R.id.explore_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
///
Log.e("explore frag","1");
db.collection("Users").document(id).collection("preferences").document("event").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
String anime = documentSnapshot.get("anime").toString();
String art = documentSnapshot.get("art").toString();
String edu = documentSnapshot.get("edu").toString();
String gaming = documentSnapshot.get("gaming").toString();
String music = documentSnapshot.get("music").toString();
String other = documentSnapshot.get("other").toString();
String all = documentSnapshot.get("all").toString();
String search_key = search_keyword.getText().toString().trim();
country = ccp.getSelectedCountryName();
if (all.equals("1") && TextUtils.isEmpty(search_key)) {
// setUpRecyclerView(country);
Query query = Cref.whereEqualTo("country", country);
FirestoreRecyclerOptions<POJO_explore_page_item> options = new
FirestoreRecyclerOptions.Builder<POJO_explore_page_item>()
.setQuery(query, POJO_explore_page_item.class).
build();
adapter = new ExplorePageAdapter(options);
recyclerView.setAdapter(null);
recyclerView.setLayoutManager(null);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
Log.e("explore frag","2");
} else if (all.equals("1") && !(TextUtils.isEmpty(search_key))) {
setUpRecyclerViewWithKeyword(country, search_key);
Log.e("explore frag","3");
} else if (all.equals("0") && TextUtils.isEmpty(search_key)) {
setUpRecyclerViewWithPreferences(country, anime, art, edu, gaming, music, other);
Log.e("explore frag","4");
} else {
setUpRecyclerViewWithKeywordAndPreferences(country, search_key, anime, art, edu, gaming, music, other);
Log.e("explore frag","5");
}
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
adapter.setOnItemClickListener(
new ExplorePageAdapter.OnItemClickListener() {
@Override
public void OnItemClick(DocumentSnapshot documentSnapshot, int position) {
String id = documentSnapshot.getId();
Intent intent = new Intent(getActivity(),
event_customer_view_activity.class);
intent.putExtra("Event_ID", id);
startActivity(intent);
// getActivity().finish();
}
}
);
}
Neither of these two attempts worked. Any suggestions?
答案1
得分: 1
解决方案
正如 @just_user 的原帖第二条评论所示,我需要在创建新的适配器之前调用 adapter.stopListening()
。之后,我可以调用 adapter.startListening()
。具体示例如下:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
search_keyword = getView().findViewById(R.id.exp_search);
search_btn = getView().findViewById(R.id.exp_search_btn);
ccp = getView().findViewById(R.id.exp_ccp);
refresh = getView().findViewById(R.id.refresh);
country = ccp.getSelectedCountryName();
setUpRecyclerViewInitial(country);
db.collection("Users").document(id).collection("preferences").document("event").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
String anime = documentSnapshot.get("anime").toString();
String art = documentSnapshot.get("art").toString();
String edu = documentSnapshot.get("edu").toString();
String gaming = documentSnapshot.get("gaming").toString();
String music = documentSnapshot.get("music").toString();
String other = documentSnapshot.get("other").toString();
String all = documentSnapshot.get("all").toString();
String search_key = search_keyword.getText().toString().trim();
country = ccp.getSelectedCountryName();
adapter.stopListening();
RebuildRecyclerView(country);
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
adapter.setOnItemClickListener(
new ExplorePageAdapter.OnItemClickListener() {
@Override
public void OnItemClick(DocumentSnapshot documentSnapshot, int position) {
String id = documentSnapshot.getId();
Intent intent = new Intent(getActivity(),
event_customer_view_activity.class);
intent.putExtra("Event_ID", id);
startActivity(intent);
}
}
);
}
private void setUpRecyclerViewInitial(String country) {
Log.e("explore frag", country);
Query query = Cref.whereEqualTo("country", country).orderBy("count", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<POJO_explore_page_item> options = new
FirestoreRecyclerOptions.Builder<POJO_explore_page_item>()
.setQuery(query, POJO_explore_page_item.class)
.build();
adapter = new ExplorePageAdapter(options);
RecyclerView recyclerView = getView().findViewById(R.id.explore_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
private void RebuildRecyclerView(String country) {
Log.e("explore frag", country);
Query query = Cref.whereEqualTo("country", country).orderBy("count", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<POJO_explore_page_item> options = new
FirestoreRecyclerOptions.Builder<POJO_explore_page_item>()
.setQuery(query, POJO_explore_page_item.class)
.build();
adapter = new ExplorePageAdapter(options);
adapter.startListening();
RecyclerView recyclerView = getView().findViewById(R.id.explore_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
public void onResume() {
super.onResume();
adapter.startListening();
}
@Override
public void onDestroy() {
super.onDestroy();
adapter.stopListening();
}
英文:
Solution
As @just_user's second comment on my original post implies, I have to call adapter.stopListening()
before I create a new adapter. Afterwards, I can call adapter.startListening()
.This is illustrated below:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final String id = FirebaseAuth.getInstance().getCurrentUser().getUid();
search_keyword = getView().findViewById(R.id.exp_search);
search_btn = getView().findViewById(R.id.exp_search_btn);
ccp = getView().findViewById(R.id.exp_ccp);
refresh = getView().findViewById(R.id.refresh);
country = ccp.getSelectedCountryName();
setUpRecyclerViewInitial(country);
db.collection("Users").document(id).collection("preferences").document("event").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
String anime = documentSnapshot.get("anime").toString();
String art = documentSnapshot.get("art").toString();
String edu = documentSnapshot.get("edu").toString();
String gaming = documentSnapshot.get("gaming").toString();
String music = documentSnapshot.get("music").toString();
String other = documentSnapshot.get("other").toString();
String all = documentSnapshot.get("all").toString();
String search_key = search_keyword.getText().toString().trim();
country = ccp.getSelectedCountryName();
adapter.stopListening();
RebuildRecyclerView(country);
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
adapter.setOnItemClickListener(
new ExplorePageAdapter.OnItemClickListener() {
@Override
public void OnItemClick(DocumentSnapshot documentSnapshot, int position) {
String id = documentSnapshot.getId();
Intent intent = new Intent(getActivity(),
event_customer_view_activity.class);
intent.putExtra("Event_ID", id);
startActivity(intent);
// getActivity().finish();
}
}
);
}
private void setUpRecyclerViewInitial(String country) {
//.whereIn("type", Arrays.asList("Other"))
Log.e("explore frag",country);
Query query = Cref.whereEqualTo("country", country).orderBy("count", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<POJO_explore_page_item> options = new
FirestoreRecyclerOptions.Builder<POJO_explore_page_item>()
.setQuery(query, POJO_explore_page_item.class).
build();
adapter = new ExplorePageAdapter(options);
RecyclerView recyclerView = getView().findViewById(R.id.explore_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
private void RebuildRecyclerView(String country) {
//.whereIn("type", Arrays.asList("Other"))
Log.e("explore frag",country);
Query query = Cref.whereEqualTo("country", country).orderBy("count", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<POJO_explore_page_item> options = new
FirestoreRecyclerOptions.Builder<POJO_explore_page_item>()
.setQuery(query, POJO_explore_page_item.class).
build();
adapter = new ExplorePageAdapter(options);
adapter.startListening()
RecyclerView recyclerView = getView().findViewById(R.id.explore_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
public void onResume() {
super.onResume();
adapter.startListening();
}
@Override
public void onDestroy() {
super.onDestroy();
adapter.stopListening();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论