基本使用:studio中直接引用
compile
'com.ms-square:expandableTextView:0.1.4',然后可以在代码中使用,用法和TextView一样。
布局文件:
<?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">
<com.ms.square.android.expandabletextview.ExpandableTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:expandableTextView="http://schemas.android.com/apk/res-auto"
android:id="@+id/expand_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:orientation="vertical"
expandableTextView:clickListenerType="all"
expandableTextView:collapseDrawable="@drawable/ic_collapse_large_holo_light"
expandableTextView:expandDrawable="@drawable/ic_expand_large_holo_light"
expandableTextView:maxCollapsedLines="2">
<TextView
android:id="@id/expandable_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:textColor="#000" />
<RelativeLayout
android:id="@+id/expand_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@id/expand_collapse"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@android:color/transparent"
android:clickable="false" />
</RelativeLayout>
</com.ms.square.android.expandabletextview.ExpandableTextView>
</LinearLayout>
ExpandableTextView的一些属性:
maxCollapsedLines 当TextView 收起的时候允许显示的最大行数。expandDrawable 展开按钮的图片
collapseDrawable 收起按钮的图片
如果是在listview中作为一个条目使用,那么在复用的时候,会出现问题,就是复用那些展开的条目,都是展开的。
解决办法如下:
在adapter中添加这行代码
private
SparseBooleanArray
mConvertTextCollapsedStatus
=
new
SparseBooleanArray()
;
设置文本的时候,不要直接用setText(list.get(position)), 用
viewHolder.
mExpandableTextView
.setText(
list
.get(position)
,
mConvertTextCollapsedStatus
,
position)
;