【问题标题】:Rebuilding the recycler view causes it to disappear重建回收站视图会导致它消失
【发布时间】:2021-01-05 05:44:43
【问题描述】:

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

我的 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); andrecyclerView.setLayoutManager(null);

  recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

我也尝试在我的 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",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();

                    }
                }
        );

    }

这两种尝试都没有奏效。有什么建议吗?

【问题讨论】:

  • 当使用 recyclerview 和适配器时,您应该只设置一次适配器。之后,如果您的内容已更改,请在适配器中设置更改的项目列表,然后调用 adapter.notifyDataSetChanged() ,您的 recyclerview 将显示更新内容的列表。
  • @just_user 我在原始帖子中使用的 FirebaseUI Firestore 回收器视图是否也是这种情况?如果有,能举个例子吗?
  • 对不起,没有。 FirestoreRecyclerAdapter 的情况并非如此。在这个中,内置了快照侦听器,当从 Firebase 接收到更改时,它会调用 notifyDataSetChanged。所以这应该自动完成。我自己从未使用过 FirestoreRecyclerAdapter,但关键是适配器。但一般来说,当您在活动中启动 adapter.startListening() 时,适配器应自行处理并自动更新列表。如果您需要将内容更改为另一个列表,请停止侦听器,创建一个新适配器,然后重新开始侦听。
  • 找到本教程:codinginflow.com/tutorials/android/…。根据 Firebase 的经验,如果您将某些内容添加到列表中,请不要尝试在本地强制更新。将其添加到 Firestore 并等待更新到达您的列表。
  • @just_user 您的第二条评论有帮助。我发布了我的解决方案的代表作为答案。

标签: java android android-recyclerview firebaseui


【解决方案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);
                        //   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();

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多