Android库:ExpandableSelector
jopen
9年前
ExpandableSelector是一个Android库用于在一个动画容器中创建一个of Button/ImageButton控件列表,支持关闭和展开。
使用说明:
在布局中添加ExpandableSelector
<com.karumi.expandableselector.ExpandableSelector android:id="@+id/es_sizes" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
在Activity或者Fragment的某个生命周期中创建一个ExpandableItem的list,然后把它们用在ExpandableSelector控件中:
private void initializeSizesExpandableSelector() { ExpandableSelector sizesExpandableSelector = (ExpandableSelector) findViewById(R.id.es_sizes); List<ExpandableItem> expandableItems = new ArrayList<ExpandableItem>(); expandableItems.add(new ExpandableItem("XL")); expandableItems.add(new ExpandableItem("L")); expandableItems.add(new ExpandableItem("M")); expandableItems.add(new ExpandableItem("S")); sizesExpandableSelector.showExpandableItems(expandableItems); }
通过ExpandableSelectorListener来监听动画事件:
private void configureExpandableSelectorListener() { sizesExpandableSelector.setExpandableSelectorListener(new ExpandableSelectorListener() { @Override public void onCollapse() { //Do something here } @Override public void onExpand() { //Do something here } @Override public void onCollapsed() { //Do something here } @Override public void onExpanded() { //Do something here } }); }
通过OnExpandableItemClickListener来监听点击事件:
private void configureExpandableSelectorClickListener() { sizesExpandableSelector.setOnExpandableItemClickListener(new OnExpandableItemClickListener() { @Override public void onExpandableItemClickListener(int index, View view) { //Do something here } }); }
自定义
可以轻松的实现ExpandableItem的自定义。只需在主题中设置一个expandableItemSyle 属性,然后让这个属性引用一个自定义的样式。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="expandableItemStyle">@style/ExpandableItemStyle</item> </style> <style name="ExpandableItemStyle"> <item name="android:layout_height">@dimen/item_size</item> <item name="android:layout_width">@dimen/item_size</item> <item name="android:background">@drawable/bg_item_with_title</item> <item name="android:layout_margin">@dimen/item_margin</item> <item name="android:textColor">@color/gray</item> <item name="android:textSize">@dimen/item_text_size</item> </style>