Android實現(xiàn)的ListView分組布局改進(jìn)示例
本文實例講述了Android實現(xiàn)的ListView分組布局改進(jìn)方法。分享給大家供大家參考,具體如下:
由于是在網(wǎng)上轉(zhuǎn)載的一篇文章,在這里就不多說廢話了,首先看一下最終的效果圖:
然后是實現(xiàn)該ListView布局的主要代碼:
1、程序主界面 SeparateListView.java
package whu.iss.wuxianglong; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class SeparateListView extends Activity { ListView listView; MyAdapter myAdapter; public List<String> listTag = new ArrayList<String>(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView) findViewById(R.id.list); myAdapter = new MyAdapter(this, android.R.layout.simple_expandable_list_item_1, getData()); listView.setAdapter(myAdapter); } private List<String> getData() { List<String> data = new ArrayList<String>(); int i = 0; data.add("A"); listTag.add("A"); data.add("aa試數(shù)據(jù)" + (i++)); data.add("a試數(shù)據(jù)" + (i++)); data.add("aa試數(shù)據(jù)" + (i++)); listTag.add("B"); data.add("B"); data.add("bb試數(shù)據(jù)" + (i++)); data.add("b試數(shù)據(jù)" + (i++)); data.add("b試數(shù)據(jù)" + (i++)); data.add("b試數(shù)據(jù)" + (i++)); listTag.add("C"); data.add("C"); data.add("c測試數(shù)據(jù)" + (i++)); data.add("c測試數(shù)據(jù)" + (i++)); listTag.add("D"); data.add("D"); data.add("d測試數(shù)據(jù)" + (i++)); data.add("d測試數(shù)據(jù)" + (i++)); data.add("d測試數(shù)據(jù)" + (i++)); listTag.add("E"); data.add("E"); data.add("e測試數(shù)據(jù)" + (i++)); data.add("e測試數(shù)據(jù)" + (i++)); data.add("e測試數(shù)據(jù)" + (i++)); listTag.add("F"); data.add("F" ); data.add("f測試數(shù)據(jù)" + (i++)); return data; } class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, int textViewResourceId, List<String> objects) { super(context, textViewResourceId, objects); } @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEnabled(int position) { // 如果-開頭,則該項不可選 return !listTag.contains(getItem(position)); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; //根據(jù)標(biāo)簽類型加載不通的布局模板 if(listTag.contains(getItem(position))){ //如果是標(biāo)簽項 view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_tag, null); }else{ //否則就是數(shù)據(jù)項 view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item, null); } //顯示名稱 TextView textView = (TextView) view.findViewById(R.id.group_list_item_text); textView.setText(getItem(position)); //返回重寫的view return view; } } }
2、程序主界面布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
3、ListView中數(shù)據(jù)部分樣式布局文件group_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dip"> <ImageView android:src="@drawable/icon" android:layout_width="50px" android:layout_height="50px"> </ImageView> <TextView android:id="@+id/group_list_item_text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:paddingLeft="5dip" android:gravity="center_vertical"> </TextView> </LinearLayout>
4、ListView中分組標(biāo)志行的樣式布局文件group_list_item_tag.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#555555" android:paddingLeft="10dip"> <TextView android:id="@+id/group_list_item_text" android:layout_width="wrap_content" android:layout_height="20dip" android:textColor="#ffffff" android:gravity="center_vertical"> </TextView> </LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- android 通過向viewpage中添加listview來完成滑動效果(類似于qq滑動界面)
- android Listview模擬聊天界面
- Android 新聞界面模擬ListView和ViewPager的應(yīng)用
- Android ListView自定義Adapter實現(xiàn)仿QQ界面
- Android App界面的ListView布局實戰(zhàn)演練
- Android中使用Expandablelistview實現(xiàn)微信通訊錄界面
- android動態(tài)布局之動態(tài)加入TextView和ListView的方法
- Android ListView添加頭布局和腳布局實例詳解
- 神奇的listView實現(xiàn)自動顯示隱藏布局Android代碼
- Android開發(fā)之ListView的簡單用法及定制ListView界面操作示例
相關(guān)文章
Android Studio 自定義Debug變量視圖的方法
這篇文章主要介紹了Android Studio 自定義Debug變量視圖的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07AndroidStudio圖片壓縮工具ImgCompressPlugin使用實例
這篇文章主要為大家介紹了AndroidStudio圖片壓縮工具ImgCompressPlugin使用實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android Studio 中aidl的自定義類的使用詳解
這篇文章主要介紹了Android Studio 中aidl的自定義類的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03PagerSlidingTabStrip制作Android帶標(biāo)簽的多界面滑動切換
這篇文章主要介紹了使用PagerSlidingTabStrip制作Android帶標(biāo)簽的多界面滑動切換效果的方法,PagerSlidingTabStrip是GitHub上的一個開源項目,調(diào)用這個庫可以少寫不少代碼XD 需要的朋友可以參考下2016-04-04Android studio實現(xiàn)簡易的計算器功能
這篇文章主要為大家詳細(xì)介紹了Android studio實現(xiàn)簡易的計算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05Android?Studio實現(xiàn)帶三角函數(shù)對數(shù)運(yùn)算功能的高級計算器
這篇文章主要為大家詳細(xì)介紹了Android?Studio實現(xiàn)帶三角函數(shù)對數(shù)運(yùn)算功能的高級計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05android 仿微信demo——注冊功能實現(xiàn)(服務(wù)端)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗?,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06Android網(wǎng)絡(luò)技術(shù)HttpURLConnection詳解
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)技術(shù)HttpURLConnection的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07