【问题标题】:My RecyclerView Adapter is not respond to updates via Volley Request?我的 RecyclerView 适配器没有通过 Volley Request 响应更新?
【发布时间】:2020-09-23 01:56:31
【问题描述】:

当我启动我的应用程序时,应该显示来自 ChatBot 的初始消息,但不是。它以一个成功的 Volley 请求开始,并且数据始终存在。

然后在 Response Handler 中调用这个:

            @Override
            public void onResponse(JSONArray response) {
                try {
                    for (int i = 0; i < response.length(); i++) {
                        String[] text = ((JSONObject) response.get(i)).getString("text").split("'");
                        chat.addChat(new ChatBubbleModel(ChatBubbleModel.UserType.BOT, text[1]));
                    }

                } catch (Exception e) {

                }
            }

chat 属性是 ChatModel 类型的属性,它是使用通过 RecyclerView.Adapter 自定义实现触发此 UI 更新的观察者模式创建的:

chat.observe(new ChatModel.Observer<ChatBubbleModel>() {

            @Override
            public void onChange(ChatBubbleModel data) {
                chatAdapter.addBubble(data);
                if (data.getUserType() == ChatBubbleModel.UserType.BOT) {
                    return;
                }
                    JSONObject requestInstance = new JSONObject();
                    try {
                        requestInstance.put("text", data.getMessage());
                    } catch (JSONException e) {

                    }

                    CustomRequest request = new CustomRequest(Request.Method.POST, "https://url.com", requestInstance, new Response.Listener<JSONArray>() {

                        @Override
                        public void onResponse(JSONArray response) {
                            try {
                                for (int i = 0; i < response.length(); i++) {
                                    String[] text = ((JSONObject) response.get(i)).getString("text").split("'");
                                    chat.addChat(new ChatBubbleModel(ChatBubbleModel.UserType.BOT, text[1]));
                                }

                            } catch (Exception e) {

                            }
                        }


                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }) {

                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            Map<String, String> headers = Maps.newHashMap();
                            headers.put("Content-Type", "application/json");
                            if (chat.getHistoryId() == null || chat.getHistoryId().isEmpty())
                                chat.setHistoryId(UUID.randomUUID().toString());
                            headers.put("historyID", chat.getHistoryId());
                            headers.put("userName", "");
                            headers.put("configurationId", "5ed0d05b95b0ba16f9690d31");
                            return headers;
                        }
                    };

                    RequestQue.getInstance().addToQueue(request);
                }

        });

但是,只有在我扩展键盘时才会更改来自 Bot(聊天气泡渲染)的所有请求,所以我认为 RecyclerView 重新渲染会被触发,因为大小发生了变化。

这是我的 RecyclerView.Adapter 中的方法:

public void addBubble(ChatBubbleModel bubble) {
        this.bubbles.add(bubble);
        notifyItemChanged(bubbles.size() - 1);
    }

但如果在 Volley 网络响应之后调用它们,则不会调用 public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) 方法。仅在键盘事件之后。

【问题讨论】:

    标签: android android-recyclerview observable android-volley


    【解决方案1】:

    请使用notifyItemInserted(bubbles.size() - 1); 而不是notifyItemChanged(bubbles.size() - 1);

    您的addBubble 方法如下:

    public void addBubble(ChatBubbleModel bubble) {
            this.bubbles.add(bubble);
            notifyItemInserted(bubbles.size() - 1);
        }
    

    【讨论】:

    • 我会在一分钟内尝试修复,希望它是修复 ? 但我确定我已经将自己暴露为 Android Noob ??
    • 这实际上是问题
    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多