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

Android Fragment中RecyclerView首次加载正常返回后白屏问题求助

问题根因定位

你的问题核心是数据重载逻辑完全错误,和Dao层实现无关,具体错误点如下:

  • 分支判断逻辑完全写反:第二个判断条件if(pref.getBoolean("firstTime",false))永远无法在非首次进入时触发,首次加载结束后你已经把firstTime标记为false,后续进入页面不会执行任何数据加载逻辑。
  • 数据库插入时机错误:Volley网络请求是异步执行的,你在发起请求后立刻开子线程插入空的data列表到数据库,就算后续修复了读取逻辑,从数据库拿到的也是空数据。
  • Adapter绑定逻辑错误:开头你注释了recyclerView.setAdapter(adapter)代码,非首次进入时RecyclerView没有绑定适配器;同时你在子线程中重复创建局部Adapter实例,全局adapter调用notifyDataSetChanged不会生效。
  • firstTime标记修改时机错误:首次进入时不管网络请求是否成功、数据是否入库,直接修改firstTime为false,后续永远不会重新拉取网络数据。
修复后参考代码
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // 初始化列表和适配器
    data = new ArrayList<>();
    adapter = new PlatformAdapter(data);
    recyclerView = view.findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(adapter); // 不要注释这行

    SharedPreferences pref = getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
    boolean isFirstTime = pref.getBoolean("firstTime", true);

    if (isFirstTime) {
        // 首次进入:先拉网络数据
        RequestVolley.getInstance(getContext())
                .doGetRequest("http://www....json", new RequestVolley.OnCompleteCallback() {
                    @Override
                    public void onCompleted(String response) {
                        try {
                            List<Platform> tempList = new ArrayList<>();
                            JSONArray jsonArray = new JSONArray(response);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject object = jsonArray.optJSONObject(i);
                                if (object != null) {
                                    tempList.add(Platform.parseJSON(object));
                                }
                            }
                            // 数据拿到后先更新UI
                            data.addAll(tempList);
                            adapter.notifyDataSetChanged();
                            // 开子线程存数据库
                            new Thread(() -> {
                                DB.getInstance(getContext()).getPlatformDao().insert(tempList);
                                // 存完再修改首次标记
                                SharedPreferences.Editor editor = pref.edit();
                                editor.putBoolean("firstTime", false);
                                editor.apply();
                            }).start();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    } else {
        // 非首次进入:直接读数据库
        new Thread(() -> {
            List<Platform> dbList = DB.getInstance(getContext()).getPlatformDao().findAll();
            data.addAll(dbList);
            // 切主线程更新UI
            recyclerView.post(() -> adapter.notifyDataSetChanged());
        }).start();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 15:06:00