Android實(shí)現(xiàn)的ListView分組布局改進(jìn)示例
本文實(shí)例講述了Android實(shí)現(xiàn)的ListView分組布局改進(jìn)方法。分享給大家供大家參考,具體如下:
由于是在網(wǎng)上轉(zhuǎn)載的一篇文章,在這里就不多說(shuō)廢話(huà)了,首先看一下最終的效果圖:

然后是實(shí)現(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測(cè)試數(shù)據(jù)" + (i++));
data.add("c測(cè)試數(shù)據(jù)" + (i++));
listTag.add("D");
data.add("D");
data.add("d測(cè)試數(shù)據(jù)" + (i++));
data.add("d測(cè)試數(shù)據(jù)" + (i++));
data.add("d測(cè)試數(shù)據(jù)" + (i++));
listTag.add("E");
data.add("E");
data.add("e測(cè)試數(shù)據(jù)" + (i++));
data.add("e測(cè)試數(shù)據(jù)" + (i++));
data.add("e測(cè)試數(shù)據(jù)" + (i++));
listTag.add("F");
data.add("F" );
data.add("f測(cè)試數(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) {
// 如果-開(kāi)頭,則該項(xiàng)不可選
return !listTag.contains(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
//根據(jù)標(biāo)簽類(lèi)型加載不通的布局模板
if(listTag.contains(getItem(position))){
//如果是標(biāo)簽項(xiàng)
view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_tag, null);
}else{
//否則就是數(shù)據(jù)項(xiàng)
view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item, null);
}
//顯示名稱(chēng)
TextView textView = (TextView) view.findViewById(R.id.group_list_item_text);
textView.setText(getItem(position));
//返回重寫(xiě)的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)容感興趣的讀者可查看本站專(zhuān)題:《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android 通過(guò)向viewpage中添加listview來(lái)完成滑動(dòng)效果(類(lèi)似于qq滑動(dòng)界面)
- android Listview模擬聊天界面
- Android 新聞界面模擬ListView和ViewPager的應(yīng)用
- Android ListView自定義Adapter實(shí)現(xiàn)仿QQ界面
- Android App界面的ListView布局實(shí)戰(zhàn)演練
- Android中使用Expandablelistview實(shí)現(xiàn)微信通訊錄界面
- android動(dòng)態(tài)布局之動(dòng)態(tài)加入TextView和ListView的方法
- Android ListView添加頭布局和腳布局實(shí)例詳解
- 神奇的listView實(shí)現(xiàn)自動(dòng)顯示隱藏布局Android代碼
- Android開(kāi)發(fā)之ListView的簡(jiǎn)單用法及定制ListView界面操作示例
相關(guān)文章
Android Studio 自定義Debug變量視圖的方法
這篇文章主要介紹了Android Studio 自定義Debug變量視圖的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
AndroidStudio圖片壓縮工具ImgCompressPlugin使用實(shí)例
這篇文章主要為大家介紹了AndroidStudio圖片壓縮工具ImgCompressPlugin使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android Studio 中aidl的自定義類(lèi)的使用詳解
這篇文章主要介紹了Android Studio 中aidl的自定義類(lèi)的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
PagerSlidingTabStrip制作Android帶標(biāo)簽的多界面滑動(dòng)切換
這篇文章主要介紹了使用PagerSlidingTabStrip制作Android帶標(biāo)簽的多界面滑動(dòng)切換效果的方法,PagerSlidingTabStrip是GitHub上的一個(gè)開(kāi)源項(xiàng)目,調(diào)用這個(gè)庫(kù)可以少寫(xiě)不少代碼XD 需要的朋友可以參考下2016-04-04
Android studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android?Studio實(shí)現(xiàn)帶三角函數(shù)對(duì)數(shù)運(yùn)算功能的高級(jí)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)帶三角函數(shù)對(duì)數(shù)運(yùn)算功能的高級(jí)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
android 仿微信demo——注冊(cè)功能實(shí)現(xiàn)(服務(wù)端)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗?,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android網(wǎng)絡(luò)技術(shù)HttpURLConnection詳解
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)技術(shù)HttpURLConnection的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

