You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView点击按钮不更新,仅软键盘开闭后才显示内容如何解决?

问题根因

你在异步请求解析完天气数据、更新了全局的weatherItems数据集之后,没有通知RecyclerView的适配器数据发生了变更,所以RecyclerView不会主动触发重绘。而你手动开关软键盘的时候会触发页面重新布局,才会让列表加载最新数据。


修复步骤

  1. 在onPostExecute方法末尾添加适配器刷新通知
    在DownLoadWeatherTask的onPostExecute方法中,遍历解析完所有天气数据、完成weatherItems添加操作之后,调用adapter.notifyDataSetChanged(),代码修改如下:
@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    if (s != null) {
        try {
            JSONObject jsonObjectMain = new JSONObject(s);
            JSONArray jsonArray = jsonObjectMain.getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                // 原有解析逻辑保持不变
                JSONObject jsonObjectDay = jsonArray.getJSONObject(i);
                String dateTime = jsonObjectDay.getString("dt_txt");
                String date = dateTime.substring(0, 10).trim();
                String time = dateTime.substring(11, 16).trim();
                String fallout = jsonObjectDay.getJSONArray("weather").getJSONObject(0).getString("description").trim();
                String degree = String.format("" + jsonObjectDay.getJSONObject("main").getInt("temp")).trim();
                String pressure = String.format("" + jsonObjectDay.getJSONObject("main").getInt("pressure")).trim();
                String humidity = String.format("" + jsonObjectDay.getJSONObject("main").getInt("humidity")).trim();
                String speedWind = String.format("" + jsonObjectDay.getJSONObject("wind").getInt("speed")).trim();
                String gustWind = String.format("" + jsonObjectDay.getJSONObject("wind").getInt("gust")).trim();
                String directionWind = String.format("" + jsonObjectDay.getJSONObject("wind").getInt("deg")).trim();
                weatherItems.add(new WeatherItem(date, time, fallout, degree, pressure, humidity, speedWind, gustWind, directionWind));
            }
            // 新增这一行,通知适配器数据更新
            adapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
  1. 修复布局测量异常(可选但建议修改)
  • activity_main.xml中RecyclerView补充底部约束,避免高度测量异常:
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerViewListForecast"
    app:layoutManager="LinearLayoutManager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editTextRequestCity"
    app:layout_constraintBottom_toBottomOf="parent"/>
  • weather_item.xml中CardView高度改为wrap_content,避免item占满整个RecyclerView高度:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cardViewItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="1dp"
    app:cardCornerRadius="1dp">
  1. 移除冗余初始化代码(可选优化)
    onCreate中初始化适配器时不需要重复创建weatherItems对象,修改如下:
// 直接使用全局已初始化的weatherItems
adapter = new WeatherAdapter(getApplicationContext(), weatherItems);

内容的提问来源于stack exchange,提问作者Vladimir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 14:06:10