为培养手感,最近在前端学习期间,总会学习疯狂Android讲义上的小Demo,用来培养Android手感,以免手生,今天联系ExpandableListView可展开的列表组件,这种效果可以说到处可见了,咋感觉这种效果会有点复杂,,不过只要理清思路,这是相当简单的.下面就讲解一下了:
ExpandableListView是ListView的子类,他可以说是在普通的ListView的基础上进行扩展的,他把应用中的列表项分为几组,每组里又可包含多个列表项.所以说ExpandableListView用法和ListView的用法非常相似,只是ExpandableListView所显示的列表项应该有ExpandableListAdapter提供.ExpandableListAdapter也是一个接口.它和Adapter也是类似的,实现ExpandableListAdapter有3种常用方式.
(1)扩展BaseExpandableListAdapter实现ExpandableListAdapter.
(2)使用SimpleExpandableListAdapter将两个List集合包装成ExpandableListAdapter
(3)使用SimpleCursorTreeAdapter将Cursor中的数据包装成ExpandableListAdapter.
下面就是通过自定义ExpandableListAdapter为ExpandableListView提供列表项,那么先看该Demo的布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ExpandableListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>上面布局就相当简单了,就是在LinearLayout中定义了一个ExpandableListView,下面就是看在程序内怎么进行设置父组件和子组件的,如下:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sav