Android UI Libs之ExpandableLayout
1. 說明
ExpandableLayout,顧名思義,可擴(kuò)展的布局,是一個(gè)可以幫助我們實(shí)現(xiàn)折疊功能的第三方庫,折疊時(shí),只顯示頭部,打開時(shí),顯示頭部與內(nèi)容。
2. 配置
在模塊中添加依賴:compile 'com.github.traex.expandablelayout:library:1.2.2'
因?yàn)樘砑右蕾嚨腶ar文件中設(shè)置了應(yīng)用程序圖標(biāo),所以我們要在清單文件AndroidManifest.xml的manifest里面添加xmlns:tools="http://schemas.android.com/tools",application里面面添加上tools:replace="android:icon",不然會(huì)有沖突。

3. 使用方法
擴(kuò)展單個(gè)內(nèi)容時(shí)使用ExpandableLayoutItem,擴(kuò)展ListView時(shí)使用ExpandableLayoutListView。
1. 擴(kuò)展單個(gè)內(nèi)容
擴(kuò)展單個(gè)內(nèi)容時(shí)的xml布局,expandable:headerLayout代表頭部,expandable:contentLayout代表內(nèi)容
xmlns:expandable="http://schemas.android.com/apk/res-auto"
<com.andexert.expandablelayout.library.ExpandableLayout
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
expandable:headerLayout="@layout/view_header"
expandable:contentLayout="@layout/view_content"
android:background="#e74c3c"/>

2. 擴(kuò)展Listview
擴(kuò)展ListView時(shí)的xml布局,expandable:headerLayout代表頭部,expandable:contentLayout代表內(nèi)容
<com.andexert.expandablelayout.library.ExpandableLayoutListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.andexert.expandablelayout.library.ExpandableLayoutListView>
ListView中item對(duì)應(yīng)的xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:expandable="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.andexert.expandablelayout.library.ExpandableLayoutItem
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
expandable:headerLayout="@layout/view_header"
expandable:contentLayout="@layout/view_content"
android:background="#e74c3c"/>
</LinearLayout>
java文件中的相關(guān)代碼:
private String[] array={"微信公眾號(hào)","Android技術(shù)漫談","Android","Android開發(fā)"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.header_text, array);
final ExpandableLayoutListView expandableLayoutListView = (ExpandableLayoutListView) findViewById(R.id.list_view);
expandableLayoutListView.setAdapter(arrayAdapter);
}

注意:如果ExpandableLayoutListView中的item中有EditView,那么ExpandableLayoutListView要設(shè)置android:windowSoftInputMode="adjustPan"來阻止自身的重繪與item的關(guān)閉