在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(); } } }); }}結語
控件支持直接代碼創建,還有更多API請觀看 ExpandView.java 內的注釋說明。
github地址:https://github.com/wkp111/ExpandView
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答