重建回收站视图导致它消失

Rebuilding the recycler view causes it to disappear

在我的代码中,我正在通过方法setUpRecyclerView(country);构建一个回收器视图,当我的片段被快速创建时,稍后我调用相同的方法(或构建回收器视图的各种其他方法,例如setUpRecyclerViewWithKeyword(country, search_key); , setUpRecyclerViewWithPreferences(country, anime, art, edu, gaming, music, other); , setUpRecyclerViewWithKeywordAndPreferences(country, search_key, anime, art, edu, gaming, music, other); ) 再次构建 recyler 视图。如下图所示:

我的 Fragments OnViewCreated 代码

  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构建方法

   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);


    }

我的问题是,每当我尝试重建新的回收站视图时,都没有结果,除非是第一次构建回收站视图。回收站视图几乎消失了。

尝试的解决方案:

我试过在

之前添加recyclerView.setAdapter(null); recyclerView.setLayoutManager(null);
  recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

我也尝试过在我的 OnViewCreated 方法中重建 recyler 视图

    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();

                    }
                }
        );

    }

这两次尝试都没有成功。有什么建议吗?

解决方案

正如@just_user 对我原来的 post 的第二条评论所暗示的那样,我必须在创建新适配器之前调用 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);
                        //   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();

    }