Android更多條目收縮展開控件ExpandView的示例代碼
在Android開發(fā)中,我們經(jīng)常使用列表控件,而有時(shí)候列表控件條目中又會(huì)是多條目數(shù)據(jù),這時(shí)候,我們無法確定每個(gè)條目的數(shù)據(jù)多少,而為了美觀,我們就希望條目統(tǒng)一高度,多數(shù)據(jù)的條目能夠進(jìn)行折疊、展開。今天,就為大家介紹一個(gè)這樣的自定義控件 ExpandView 。
效果演示圖

演示圖

Android Studio集成方式
dependencies{
compile 'com.wkp:ExpandView:1.0.4'
//Android Studio3.0+可用以下方式
//implementation 'com.wkp:ExpandView:1.0.4'
}
使用詳解
1.屬性講解
<!--每行字段數(shù)-->
<attr name="wkp_column" format="integer"/>
<!--最少顯示行數(shù)-->
<attr name="wkp_rowMin" format="integer"/>
<!--條目間距-->
<attr name="wkp_space" format="dimension"/>
<!--條目動(dòng)畫時(shí)長,0為無動(dòng)畫-->
<attr name="wkp_itemDuration" format="integer"/>
<!--條目高度-->
<attr name="wkp_itemHeight" format="dimension"/>
<!--“更多”按鈕圖片-->
<attr name="wkp_moreButtonImg" format="reference"/>
<!--“更多”按鈕文本-->
<attr name="wkp_moreButtonText" format="string"/>
<!--顯示文本模式時(shí)的條目背景色-->
<attr name="wkp_textBgColor" format="color"/>
<!--顯示文本模式時(shí)的條目文本顏色-->
<attr name="wkp_textColor" format="color"/>
<!--顯示文本模式時(shí)的文本大小-->
<attr name="wkp_textSize" format="dimension"/>
<!--顯示文本模式時(shí)的條目背景圖-->
<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 = {"雨水滴在我的外套", "已找到", "每分每秒", "來啊,互相傷害啊", "等你到天涯海角", "遇見了你才知道你對(duì)我多重要",
"123", "456", "789", "abc", "def", "收起"};
private static final String[] items1 = {"雨水滴在我的外套1", "已找到1", "每分每秒1", "來啊,互相傷害啊1", "等你到天涯海角1", "遇見了你才知道你對(duì)我多重要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);
//設(shè)置數(shù)據(jù)
expandView.setTextItems(items);
//測試當(dāng)在ListView中條目復(fù)用問題
expandView.setTextItems(items1);
//測試未展開下調(diào)用收起的效果
expandView.packUpItems();
//條目點(diǎn)擊監(jiān)聽
expandView.setOnItemClickListener(new ExpandView.OnItemClickListener() {
@Override
public void onItemClick(View view, ViewGroup parent, int position) {
if (position == items.length - 1) {
//收起隱藏條目
expandView.packUpItems();
}
}
});
}
}
結(jié)語
控件支持直接代碼創(chuàng)建,還有更多API請(qǐng)觀看 ExpandView.java 內(nèi)的注釋說明。
github地址:https://github.com/wkp111/ExpandView
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android RecyclerView實(shí)現(xiàn)點(diǎn)擊條目刪除
- Android自定義SwipeLayout仿QQ側(cè)滑條目
- Android仿京東分類模塊左側(cè)分類條目效果
- Android ListView自動(dòng)生成列表?xiàng)l目的實(shí)例
- Android XRecyclerView實(shí)現(xiàn)多條目加載
- Android條目拖拽刪除功能實(shí)例代碼
- Android ListView 條目多樣式展示實(shí)例詳解
- android RecyclerView實(shí)現(xiàn)條目Item拖拽排序與滑動(dòng)刪除
- Android中l(wèi)istview和imageview實(shí)現(xiàn)條目單選效果
- Android編程實(shí)現(xiàn)canvas繪制餅狀統(tǒng)計(jì)圖功能示例【自動(dòng)適應(yīng)條目數(shù)量與大小】
- Android中RecyclerView上拉下拉,分割線,多條目的實(shí)例代碼
- Android 中 SwipeLayout一個(gè)展示條目底層菜單的側(cè)滑控件源碼解析
- 詳解Android中實(shí)現(xiàn)ListView左右滑動(dòng)刪除條目的方法
- Android實(shí)現(xiàn)下拉展示條目效果
相關(guān)文章
詳解LeakCanary分析內(nèi)存泄露如何實(shí)現(xiàn)
這篇文章主要為大家介紹了詳解LeakCanary分析內(nèi)存泄露如何實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android中通過反射實(shí)現(xiàn)圓角ImageView代碼實(shí)例
這篇文章主要介紹了Android中通過反射實(shí)現(xiàn)圓角ImageView代碼實(shí)例,本文直接給出核心實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
Android?studio開發(fā)實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android?studio開發(fā)實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
基于popupWindow實(shí)現(xiàn)懸浮半透明效果
這篇文章主要為大家詳細(xì)介紹了基于popupWindow實(shí)現(xiàn)懸浮半透明效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android 7.0中新簽名對(duì)多渠道打包的影響詳解
這篇文章主要介紹了Android 7.0中新簽名對(duì)多渠道打包的影響,文中介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
Android Socket 線程連接openwrt與arduino單片機(jī)串口雙向通信的實(shí)例解析
這篇文章主要介紹了Android Socket 線程連接openwrt與arduino單片機(jī)串口雙向通信的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android手機(jī)信號(hào)強(qiáng)度檢測詳細(xì)介紹
這篇文章主要介紹了Android手機(jī)信號(hào)強(qiáng)度檢測的相關(guān)資料,android定義了2種信號(hào)單位:dBm和asu。具體兩種的關(guān)系本文給大家介紹非常詳細(xì),需要的朋友可以參考下2016-11-11

