当前位置:首页 > 技术知识 > 正文内容

Android让视图折叠(安卓叠加视图设置)

maynowei9个月前 (08-02)技术知识191

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技术漫谈

相关文章

C# 中的多线程同步机制:lock、Monitor 和 Mutex 用法详解

在多线程编程中,线程同步是确保多个线程安全地访问共享资源的关键技术。C# 提供了几种常用的同步机制,其中 lock、Monitor 和 Mutex 是最常用的同步工具。本文将全面介绍这三种同步机制的用...

打通 JAVA 与内核系列之 一 ReentrantLock 锁的实现原理

写JAVA代码的同学都知道,JAVA里的锁有两大类,一类是synchronized锁,一类是concurrent包里的锁(JUC锁)。其中synchronized锁是JAVA语言层面提供的能力,在此不...

C++ 原子操作与锁的深度解析:为什么原子操作并非万金油?

大噶好,我是henry,今天来和大家浅浅聊一下为啥C++原子操作并非万能钥匙,原因有三,且听我娓娓道来:一、原子操作的线程安全性C++11 的 std::atomic 确实为单个变量的线程安全操作提供...

C语言编写多线程,什么时候要使用互斥锁?为什么要使用互斥锁?

在多线程编程中,当多个线程同时访问共享资源(如变量、文件等)时,会出现竞态条件(Race Condition)问题,导致程序的行为不可预测。为了避免这种问题,需要使用互斥锁来保护共享资源的访问。互斥锁...

Oracle又双叕开始严查JDK,连夜提桶跑路

哈佛商业报道了最近几起北美的JDK许可问题公司用了来源不明的JDK,怕蹲里面,连夜提桶跑路The company uses JDK from unknown sources, and is afrai...

Oracle数据库无法连接问题排查(oracle数据库连接不成功)

数据库告警日志 如下图 。发现 问题时间段,没有 数据库服务故障 报错,但是存在较多 TNS-12535 、 12560 、 12170 、 00505 错误:通过检查问题时间段应用日志, 也记录了...