ExpandableTextView可以展开和收缩的TextView

基本使用: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是 一个容器,包含了文字显示部分TextView 以及触发展开收起的按钮(那个小箭头)ImageButton,触发按钮可以根据需求自定义,替换成自己的。

ExpandableTextView的一些属性:
maxCollapsedLines  当TextView 收起的时候允许显示的最大行数。

expandDrawable  展开按钮的图片

collapseDrawable  收起按钮的图片


使用中需要注意的问题:
        如果是在listview中作为一个条目使用,那么在复用的时候,会出现问题,就是复用那些展开的条目,都是展开的。
解决办法如下:
        在adapter中添加这行代码    private  SparseBooleanArray  mConvertTextCollapsedStatus  new  SparseBooleanArray() ;
        设置文本的时候,不要直接用setText(list.get(position)),   用 viewHolder. mExpandableTextView .setText( list .get(position) mConvertTextCollapsedStatus position) ;

猜你喜欢

转载自blog.csdn.net/huideveloper/article/details/52350438