重新构建回收视图会导致它消失。

huangapple go评论68阅读模式
英文:

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(&quot;explore frag&quot;,&quot;1&quot;);
db.collection(&quot;Users&quot;).document(id).collection(&quot;preferences&quot;).document(&quot;event&quot;).get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
@Override
public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
String anime = documentSnapshot.get(&quot;anime&quot;).toString();
String art = documentSnapshot.get(&quot;art&quot;).toString();
String edu = documentSnapshot.get(&quot;edu&quot;).toString();
String gaming = documentSnapshot.get(&quot;gaming&quot;).toString();
String music = documentSnapshot.get(&quot;music&quot;).toString();
String other = documentSnapshot.get(&quot;other&quot;).toString();
String all = documentSnapshot.get(&quot;all&quot;).toString();
String search_key = search_keyword.getText().toString().trim();
country = ccp.getSelectedCountryName();
if (all.equals(&quot;1&quot;) &amp;&amp; TextUtils.isEmpty(search_key)) {
setUpRecyclerView(country);
Log.e(&quot;explore frag&quot;,&quot;2&quot;);
} else if (all.equals(&quot;1&quot;) &amp;&amp; !(TextUtils.isEmpty(search_key))) {
setUpRecyclerViewWithKeyword(country, search_key);
Log.e(&quot;explore frag&quot;,&quot;3&quot;);
} else if (all.equals(&quot;0&quot;) &amp;&amp; TextUtils.isEmpty(search_key)) {
setUpRecyclerViewWithPreferences(country, anime, art, edu, gaming, music, other);
Log.e(&quot;explore frag&quot;,&quot;4&quot;);
} else {
setUpRecyclerViewWithKeywordAndPreferences(country, search_key, anime, art, edu, gaming, music, other);
Log.e(&quot;explore frag&quot;,&quot;5&quot;);
}
}
}
}
}).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(&quot;Event_ID&quot;, id);
startActivity(intent);
//   getActivity().finish();
}
}
);
}

RecylerView building method

   private void setUpRecyclerView(String country) {
Log.e(&quot;explore frag&quot;,country);
Query query = Cref.whereEqualTo(&quot;country&quot;, country);
FirestoreRecyclerOptions&lt;POJO_explore_page_item&gt; options = new
FirestoreRecyclerOptions.Builder&lt;POJO_explore_page_item&gt;()
.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(&quot;explore frag&quot;,country);
Query query = Cref.whereEqualTo(&quot;country&quot;, country);
FirestoreRecyclerOptions&lt;POJO_explore_page_item&gt; options = new
FirestoreRecyclerOptions.Builder&lt;POJO_explore_page_item&gt;()
.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(&quot;explore frag&quot;,&quot;1&quot;);
db.collection(&quot;Users&quot;).document(id).collection(&quot;preferences&quot;).document(&quot;event&quot;).get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
@Override
public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
String anime = documentSnapshot.get(&quot;anime&quot;).toString();
String art = documentSnapshot.get(&quot;art&quot;).toString();
String edu = documentSnapshot.get(&quot;edu&quot;).toString();
String gaming = documentSnapshot.get(&quot;gaming&quot;).toString();
String music = documentSnapshot.get(&quot;music&quot;).toString();
String other = documentSnapshot.get(&quot;other&quot;).toString();
String all = documentSnapshot.get(&quot;all&quot;).toString();
String search_key = search_keyword.getText().toString().trim();
country = ccp.getSelectedCountryName();
if (all.equals(&quot;1&quot;) &amp;&amp; TextUtils.isEmpty(search_key)) {
//  setUpRecyclerView(country);
Query query = Cref.whereEqualTo(&quot;country&quot;, country);
FirestoreRecyclerOptions&lt;POJO_explore_page_item&gt; options = new
FirestoreRecyclerOptions.Builder&lt;POJO_explore_page_item&gt;()
.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(&quot;explore frag&quot;,&quot;2&quot;);
} else if (all.equals(&quot;1&quot;) &amp;&amp; !(TextUtils.isEmpty(search_key))) {
setUpRecyclerViewWithKeyword(country, search_key);
Log.e(&quot;explore frag&quot;,&quot;3&quot;);
} else if (all.equals(&quot;0&quot;) &amp;&amp; TextUtils.isEmpty(search_key)) {
setUpRecyclerViewWithPreferences(country, anime, art, edu, gaming, music, other);
Log.e(&quot;explore frag&quot;,&quot;4&quot;);
} else {
setUpRecyclerViewWithKeywordAndPreferences(country, search_key, anime, art, edu, gaming, music, other);
Log.e(&quot;explore frag&quot;,&quot;5&quot;);
}
}
}
}
}).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(&quot;Event_ID&quot;, 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(&quot;Users&quot;).document(id).collection(&quot;preferences&quot;).document(&quot;event&quot;).get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
@Override
public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
String anime = documentSnapshot.get(&quot;anime&quot;).toString();
String art = documentSnapshot.get(&quot;art&quot;).toString();
String edu = documentSnapshot.get(&quot;edu&quot;).toString();
String gaming = documentSnapshot.get(&quot;gaming&quot;).toString();
String music = documentSnapshot.get(&quot;music&quot;).toString();
String other = documentSnapshot.get(&quot;other&quot;).toString();
String all = documentSnapshot.get(&quot;all&quot;).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(&quot;Event_ID&quot;, id);
startActivity(intent);
//   getActivity().finish();
}
}
);
}
private void setUpRecyclerViewInitial(String country) {
//.whereIn(&quot;type&quot;, Arrays.asList(&quot;Other&quot;))
Log.e(&quot;explore frag&quot;,country);
Query query = Cref.whereEqualTo(&quot;country&quot;, country).orderBy(&quot;count&quot;, Query.Direction.ASCENDING);
FirestoreRecyclerOptions&lt;POJO_explore_page_item&gt; options = new
FirestoreRecyclerOptions.Builder&lt;POJO_explore_page_item&gt;()
.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(&quot;type&quot;, Arrays.asList(&quot;Other&quot;))
Log.e(&quot;explore frag&quot;,country);
Query query = Cref.whereEqualTo(&quot;country&quot;, country).orderBy(&quot;count&quot;, Query.Direction.ASCENDING);
FirestoreRecyclerOptions&lt;POJO_explore_page_item&gt; options = new
FirestoreRecyclerOptions.Builder&lt;POJO_explore_page_item&gt;()
.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();
}

huangapple
  • 本文由 发表于 2020年9月18日 17:35:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63953152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定