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)mConvertTextCollapsedStatusposition);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章