|  |  | 
 |  |  |     fun setButtonId(id: String) { | 
 |  |  |         this.buttonId = id | 
 |  |  |         // 加载保存的状态 | 
 |  |  |         loadStates() | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 保存所有图例项的状态 | 
 |  |  |      */ | 
 |  |  |     private fun saveStates() { | 
 |  |  |         val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) | 
 |  |  |         val states = legendItems.map { it.isSelected } | 
 |  |  |         prefs.edit().putString("${KEY_LEGEND_STATES}_$buttonId", states.joinToString(",")).apply() | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 加载保存的状态 | 
 |  |  |      */ | 
 |  |  |     private fun loadStates() { | 
 |  |  |         val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) | 
 |  |  |         val savedStates = prefs.getString("${KEY_LEGEND_STATES}_$buttonId", null) | 
 |  |  |          | 
 |  |  |         if (savedStates != null && legendItems.isNotEmpty()) { | 
 |  |  |             val states = savedStates.split(",").map { it.toBoolean() } | 
 |  |  |             states.forEachIndexed { index, state -> | 
 |  |  |                 if (index < legendItems.size) { | 
 |  |  |                     legendItems[index].isSelected = state | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             invalidate() | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 设置图例内容 | 
 |  |  |      */ | 
 |  |  |     @JvmName("setLegendsList") | 
 |  |  |     fun setLegends(items: List<Triple<Drawable, Drawable, String>>) { | 
 |  |  |         legendItems = items.map { (selectedIcon, unselectedIcon, description) -> | 
 |  |  |     fun setLegends(items: List<Quadruple<Drawable, Drawable, String, Boolean>>) { | 
 |  |  |         legendItems = items.map { (selectedIcon, unselectedIcon, description, isSelected) -> | 
 |  |  |             selectedIcon.setBounds(0, 0, iconSize, iconSize) | 
 |  |  |             unselectedIcon.setBounds(0, 0, iconSize, iconSize) | 
 |  |  |             LegendItem(selectedIcon, unselectedIcon, description) | 
 |  |  |             LegendItem(selectedIcon, unselectedIcon, description, isSelected) | 
 |  |  |         } | 
 |  |  |          | 
 |  |  |         // 加载保存的状态 | 
 |  |  |         loadStates() | 
 |  |  |          | 
 |  |  |         if (!isExpanded) { | 
 |  |  |             text = collapsedText | 
 |  |  | 
 |  |  |  | 
 |  |  |     // 添加一个 Java 友好的方法 | 
 |  |  |     @JvmName("setLegendsArray") | 
 |  |  |     fun setLegends(vararg items: Triple<Drawable, Drawable, String>) { | 
 |  |  |     fun setLegends(vararg items: Quadruple<Drawable, Drawable, String, Boolean>) { | 
 |  |  |         setLegends(items.toList()) | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     // 添加一个数据类来表示四元组 | 
 |  |  |     data class Quadruple<A, B, C, D>( | 
 |  |  |         val first: A, | 
 |  |  |         val second: B, | 
 |  |  |         val third: C, | 
 |  |  |         val fourth: D | 
 |  |  |     ) | 
 |  |  |  | 
 |  |  |     // 添加一个便捷的扩展函数来创建 Quadruple | 
 |  |  |     fun <A, B, C, D> quadrupleOf(first: A, second: B, third: C, fourth: D): Quadruple<A, B, C, D> { | 
 |  |  |         return Quadruple(first, second, third, fourth) | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |             index,  | 
 |  |  |             legendItems[index].isSelected | 
 |  |  |         ) | 
 |  |  |          | 
 |  |  |         // 保存状态 | 
 |  |  |         saveStates() | 
 |  |  |          | 
 |  |  |  | 
 |  |  |         invalidate() | 
 |  |  |     } | 
 |  |  |  |