Kotlin实现Activity切换保存RecyclerView CheckBox状态
Android待办应用RecyclerView CheckBox状态持久化实现
问题场景
- 开发环境:Android Studio,开发语言为Kotlin
- 应用用途:记录工作待办任务
- 现有架构:基于RecyclerView实现多组待办清单,每组待办清单对应独立Activity,支持用户在不同清单页面间切换
- 待解决问题:实现CheckBox选中状态持久化存储,确保切换Activity后,已勾选的CheckBox状态不会丢失
- 初始版本适配器代码(仅实现基础CheckBox交互,无持久化能力):
package com.mattkalichman.coffee import android.util.SparseBooleanArray import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.CheckBox import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.checkbox_row.view.* class ThreeHourPullAdapter(var list: ArrayList<Data>) : RecyclerView.Adapter<ThreeHourPullAdapter.ViewHolder>() { private var titles = arrayOf( "Log into the handheld under Pull to Thaw.", "Count anything left on pastry cart (BOH).", "Count everything on front pastry cart (FOH).", "Put remaining pastries from BOH on FOH cart. Remember to FIFO!", "Pull from freezer to BOH cart. Adjust number of pulled items according to inventory.", "When pull is done, press complete pull at the bottom of the screen.", "Date all pastries with sticker gun by standards. (Marshmallow Dream Bars, Madelines, Chocolate Madelines, All Other Pastries)", "Make sure to hang sticker gun back on cart & plug handheld back in." ) var checkBoxStateArray = SparseBooleanArray() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val context = parent.context val inflater = LayoutInflater.from(context) val view = inflater.inflate(R.layout.checkbox_row, parent, false) return ViewHolder(view) } override fun getItemCount(): Int = list.size override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.checkbox.isChecked = checkBoxStateArray.get(position, false) holder.checkbox.text = titles[position] } inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { var checkbox: CheckBox = itemView.checkbox init { checkbox.setOnClickListener { if (!checkBoxStateArray.get(adapterPosition, false)) { checkbox.isChecked = true checkBoxStateArray.put(adapterPosition, true) } else { checkbox.isChecked = false checkBoxStateArray.put(adapterPosition, false) } } } } }
实现方案
通过SharedPreferences实现选中状态的本地持久化,经实测功能可正常运行,核心修改为调整ViewHolder的实现逻辑,修正后的ViewHolder核心代码如下:
注意:使用前需要在文件头部导入对应依赖类
import android.content.Context import android.content.SharedPreferences
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { var checkbox: CheckBox = itemView.checkbox var sharedPreferences: SharedPreferences = itemView.getContext().getSharedPreferences("ThreeHourPull", Context.MODE_PRIVATE) // 初始化对应清单的SharedPreferences存储实例 val editor: SharedPreferences.Editor = sharedPreferences.edit() // 初始化存储编辑器实例 init { for (i in 1..11) { checkBoxStateArray[i - 1] = sharedPreferences.getBoolean("Checkbox" + i, false) } // Activity启动时从本地读取已保存的勾选状态,回填到状态数组 checkbox.setOnClickListener { if (!checkBoxStateArray.get(adapterPosition, false)) { checkbox.isChecked = true checkBoxStateArray.put(adapterPosition, true) } else { checkbox.isChecked = false checkBoxStateArray.put(adapterPosition, false) } editor.apply { // 将所有CheckBox的最新状态写入SharedPreferences持久化保存 for (i in 1..11) { editor.putBoolean("Checkbox" + i, checkBoxStateArray[i - 1]) } editor.apply() } } } }
内容的提问来源于stack exchange,提问作者MajorasMatt
相关产品推荐
相关产品推荐

