Android更多條目收縮展開控制元件ExpandView
阿新 • • 發佈:2019-01-23
在Android開發中,我們經常使用列表控制元件,而有時候列表控制元件條目中又會是多條目資料,這時候,我們無法確定每個條目的資料多少,而為了美觀,我們就希望條目統一高度,多資料的條目能夠進行摺疊、展開。今天,就為大家介紹一個這樣的自定義控制元件ExpandView。
效果演示圖
Android Studio整合方式
dependencies{
compile 'com.wkp:ExpandView:1.0.4'
//Android Studio3.0+可用以下方式
//implementation 'com.wkp:ExpandView:1.0.4'
}
使用詳解
- 1.屬性講解
<!--每行欄位數-->
<attr name="wkp_column" format="integer"/>
<!--最少顯示行數-->
<attr name="wkp_rowMin" format="integer"/>
<!--條目間距-->
<attr name="wkp_space" format="dimension"/>
<!--條目動畫時長,0為無動畫-->
<attr name="wkp_itemDuration" format="integer"/>
<!--條目高度-->
<attr name="wkp_itemHeight" format="dimension"/>
<!--“更多”按鈕圖片-->
<attr name="wkp_moreButtonImg" format="reference"/>
<!--“更多”按鈕文字-->
<attr name="wkp_moreButtonText" format="string"/>
<!--顯示文字模式時的條目背景色-->
<attr name="wkp_textBgColor" format="color"/>
<!--顯示文字模式時的條目文字顏色-->
<attr name="wkp_textColor" format="color"/>
<!--顯示文字模式時的文字大小-->
<attr name="wkp_textSize" format="dimension"/>
<!--顯示文字模式時的條目背景圖-->
<attr name="wkp_textBgRes" format="reference"/>
- 2.佈局示例
圖1佈局
<com.wkp.expandview_lib.view.ExpandView
app:wkp_textSize="@dimen/size_16sp"
app:wkp_column="3"
app:wkp_rowMin="3"
app:wkp_itemHeight="120dp"
app:wkp_textBgRes="@drawable/text_bg"
android:id="@+id/ev"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.wkp.expandview_lib.view.ExpandView>
圖2佈局
<com.wkp.expandview_lib.view.ExpandView
app:wkp_textSize="@dimen/size_16sp"
app:wkp_column="4"
app:wkp_rowMin="2"
app:wkp_itemHeight="120dp"
app:wkp_textBgRes="@drawable/text_bg"
android:id="@+id/ev"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.wkp.expandview_lib.view.ExpandView>
- 3.程式碼示例
public class MainActivity extends AppCompatActivity {
private static final String[] items = {"雨水滴在我的外套", "已找到", "每分每秒", "來啊,互相傷害啊", "等你到天涯海角", "遇見了你才知道你對我多重要",
"123", "456", "789", "abc", "def", "收起"};
private static final String[] items1 = {"雨水滴在我的外套1", "已找到1", "每分每秒1", "來啊,互相傷害啊1", "等你到天涯海角1", "遇見了你才知道你對我多重要1",
"123", "456", "789", "abc1", "def1", "收起1"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ExpandView expandView = (ExpandView) findViewById(R.id.ev);
//設定資料
expandView.setTextItems(items);
//測試當在ListView中條目複用問題
expandView.setTextItems(items1);
//測試未展開下呼叫收起的效果
expandView.packUpItems();
//條目點選監聽
expandView.setOnItemClickListener(new ExpandView.OnItemClickListener() {
@Override
public void onItemClick(View view, ViewGroup parent, int position) {
if (position == items.length - 1) {
//收起隱藏條目
expandView.packUpItems();
}
}
});
}
}