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

BottomSheetDialogFragment关闭后RecyclerView新增项丢失问题求助

问题描述

点击按钮弹出BottomSheetDialogFragment展示预设的公司名称列表,支持通过弹窗添加新的公司名称,添加时列表能正常更新,但关闭BottomSheetDialogFragment后,新增的公司名称会丢失。

相关代码

Item类

public class Item {
    public String companyName;

    public Item(String companyName) {
        this.companyName = companyName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
}

Adapter类

public class CustomAdapter_1 extends RecyclerView.Adapter<CustomAdapter_1.myViewHolder> {
    List<Item> customAdapters;
    Context context;

    public CustomAdapter_1(Context context, List<Item> customAdapters) {
        this.customAdapters = customAdapters;
        this.context = context;
    }

    @NonNull
    @Override
    public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_adapter_1, parent, false);
        return new myViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
        holder.radioButton.setText(customAdapters.get(position).getCompanyName());
    }

    @Override
    public int getItemCount() {
        return customAdapters.size();
    }

    public class myViewHolder extends RecyclerView.ViewHolder {
        RadioButton radioButton;
        TextView textView;

        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            radioButton = itemView.findViewById(R.id.rdbtn1);
        }
    }

    public void AddItem(Item item, int position) {
        customAdapters.add(position, item);
    }
}

Fragment类

public class CustomFragment_1 extends BottomSheetDialogFragment {
    private RecyclerView recyclerView;
    private CustomAdapter_1 mAdapter;
    ArrayList<Item> listOfItem = new ArrayList<>();

    String cName;
    Button btn;
    FlatDialog flatDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_custom_1, container, false);

        recyclerView = view.findViewById(R.id.customRV);
        btn = view.findViewById(R.id.button1);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);

        mAdapter = new CustomAdapter_1(getContext(), listOfItem);
        recyclerView.setAdapter(mAdapter);

        listOfItem.add(new Item("company 1"));
        listOfItem.add(new Item("company 2"));
        listOfItem.add(new Item("company 3"));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getList();
            }
        });
        return view;
    }

    public void getList() {
        flatDialog = new FlatDialog(getContext());
        flatDialog.setTitle("Company Name")
                .setSubtitle("Please add the name of the company")
                .setFirstTextFieldHint("Company Name")
                .setFirstButtonText("ADD")
                .setSecondButtonText("CANCEL")
                .withFirstButtonListner(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listOfItem.add(new Item(flatDialog.getFirstTextField()));
                        mAdapter.notifyDataSetChanged();
                        flatDialog.dismiss();
                    }
                })
                .withSecondButtonListner(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        flatDialog.dismiss();
                    }
                })
                .show();
    }
}

解决思路

  • 数据未持久化:新增的公司名称仅存于Fragment内部的内存集合中,Fragment销毁后数据直接丢失。需要将数据持久化到本地存储,比如:
    • 用SharedPreferences:每次添加新公司后,把列表转成JSON字符串写入;Fragment创建时读取JSON并还原列表。
    • 用SQLite/Room:建立本地数据库表存储公司名称,增删改查操作直接操作数据库,Fragment每次加载时从数据库拉取最新列表。
  • Fragment重建丢失数据:若因配置变更(如屏幕旋转)导致Fragment重建,内存中的列表会被清空。可通过onSaveInstanceState保存状态:
    • 在onSaveInstanceState方法中,把列表里的公司名称提取为字符串数组存入Bundle;在onCreateView中判断Bundle是否非空,取出数组恢复列表。
  • 改用ViewModel管理数据:将列表放到ViewModel中,ViewModel的生命周期长于Fragment,配置变更或Fragment销毁重建时数据不会丢失:
    • 创建CompanyViewModel类,内部持有MutableLiveData<List<Item>>;Fragment中观察该LiveData,数据变化时自动更新Adapter;添加新公司时直接更新ViewModel中的列表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 17:51:22