|  |  | 
 |  |  |     // 添加一个属性定义三角形图标的点击区域扩展范围 | 
 |  |  |     private val triangleClickPadding: Float = 15f * context.resources.displayMetrics.density // 20dp | 
 |  |  |  | 
 |  |  |     // 添加一个标识符,用于区分不同的 ExpandButton 实例 | 
 |  |  |     private var buttonId: String = "default" | 
 |  |  |  | 
 |  |  |     companion object { | 
 |  |  |         private const val PREFS_NAME = "expand_button_prefs" | 
 |  |  |         private const val KEY_LEGEND_STATES = "legend_states" | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     init { | 
 |  |  |         // 保存 XML 中设置的默认字体大小 | 
 |  |  |         defaultTextSize = textSize | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 设置按钮的唯一标识符 | 
 |  |  |      * @param id 标识符 | 
 |  |  |      */ | 
 |  |  |     fun setButtonId(id: String) { | 
 |  |  |         this.buttonId = id | 
 |  |  |         // 加载保存的状态 | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 设置图例内容 | 
 |  |  |      */ | 
 |  |  |     @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) | 
 |  |  |         } | 
 |  |  |          | 
 |  |  |         if (!isExpanded) { | 
 |  |  | 
 |  |  |  | 
 |  |  |     // 添加一个 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 | 
 |  |  |         ) | 
 |  |  |  | 
 |  |  |         invalidate() | 
 |  |  |     } | 
 |  |  |  |