Android UI Libs之ExpandableLayout

Android UI Libs之ExpandableLayout


1. 说明


ExpandableLayout,顾名思义,可扩展的布局,是一个可以帮助我们实现折叠功能的第三方库,折叠时,只显示头部,打开时,显示头部与内容。

2. 配置


在模块中添加依赖:compile 'com.github.traex.expandablelayout:library:1.2.2'

因为添加依赖的aar文件中设置了应用程序图标,所以我们要在清单文件AndroidManifest.xmlmanifest里面添加xmlns:tools="http://schemas.android.com/tools",application里面面添加上tools:replace="android:icon",不然会有冲突。

3. 使用方法


扩展单个内容时使用ExpandableLayoutItem,扩展ListView时使用ExpandableLayoutListView

1. 扩展单个内容


扩展单个内容时的xml布局,expandable:headerLayout代表头部,expandable:contentLayout代表内容
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. 扩展Listview


扩展ListView时的xml布局,expandable:headerLayout代表头部,expandable:contentLayout代表内容

    <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对应的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文件中的相关代码:

    private String[] array={"微信公众号","Android技术漫谈","Android","Android开发"};
    @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要设置android:windowSoftInputMode="adjustPan"来阻止自身的重绘与item的关闭

程序源代码下载:https://github.com/lavor-zl/UILibs

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,213评论 25 708
  • 太长了,还是转载吧...今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源...
    庞哈哈哈12138阅读 20,264评论 3 283
  • 春和日月,风暖园林。冬季的气温一日叠着一日,终于攒成了一个草长莺飞的春天。 周末早晨醒来,迫不及待地掀开窗帘,又是...
    _清澄_阅读 536评论 6 6
  • 蒲甘,位于缅甸中部区域,坐落在伊洛瓦底河中游左岸部分,是缅甸著名的宗教古城,也是东南亚最重要的佛教遗址之一。 蒲甘...
    玉珠阳光阅读 1,470评论 0 0
  • 未来不止是说说。它很艰辛。两个人在一起的未来,会比一个人艰辛很多。这个,我是知道的,而且从来很清晰的知道。其实单身...
    静听岁月阅读 213评论 1 2