欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pp列表之分組ListView詳解

 更新時間:2013年06月17日 10:00:36   作者:  
本篇文章是對Android中的ListView進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

吸引用戶的眼球,是我們至死不渝的追求;
第一時間呈現(xiàn)最有價值的信息,簡明大方,告訴客戶,你的選擇是多么的明智,這正是你尋覓已久的東西。

分組的應(yīng)用場合還是很多的,有數(shù)據(jù)集合的地方往往要分組顯示;
分組的形式也很多,最常見的就是鑲嵌在列表中,網(wǎng)上說的很多ExpandListView的也是一種。
Android自帶的通訊錄中的聯(lián)系人是按照拼音首字母(A,B,C,D......)分組分類的,效果如下:


我們今天也是要實(shí)現(xiàn)這樣類似的一個效果。
1.樣本數(shù)據(jù):
為了突出重點(diǎn),直擊要點(diǎn),這里提供一個整理好的數(shù)據(jù)樣本:

復(fù)制代碼 代碼如下:

//list:數(shù)據(jù)集合
private List<String> list = new ArrayList<String>();
//listTag:Tag集合,其中Tag是分類的分割標(biāo)簽,每個分組的header
private List<String> listTag = new ArrayList<String>();
public void setData(){
list.add("A");
listTag.add("A");
for(int i=0;i<3;i++){
list.add("阿凡達(dá)"+i);
}
list.add("B");
listTag.add("B");
for(int i=0;i<3;i++){
list.add("比特風(fēng)暴"+i);
}
list.add("C");
listTag.add("C");
for(int i=0;i<30;i++){
list.add("查理風(fēng)云"+i);
}
}

2.Activity布局準(zhǔn)備:
放置一個listView來呈現(xiàn)數(shù)據(jù)。
group_list_activity.xml:
復(fù)制代碼 代碼如下:

<?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/group_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"/>
</LinearLayout>

3.自定義Adapter(本文繼承ArrayAdapter):
這個是本文的重點(diǎn)和核心。
Adapter接口為數(shù)據(jù)和界面搭建了一個訪問的橋梁,最重要的就是getView()方法,用這個方法我們可以實(shí)現(xiàn)一定程度的界面自定義。
ArrayAdapter間接實(shí)現(xiàn)了Adapter接口,這里我們簡單起見,數(shù)據(jù)源只是提供單一的String數(shù)組。
復(fù)制代碼 代碼如下:

private static class GroupListAdapter extends ArrayAdapter<String>{
//存放標(biāo)簽的列表,用來判斷數(shù)據(jù)項(xiàng)的類型
//如果數(shù)據(jù)項(xiàng)在標(biāo)簽列表中,則是標(biāo)簽項(xiàng),否則是數(shù)據(jù)項(xiàng)
private List<String> listTag = null;
public GroupListAdapter(Context context, List<String> objects, List<String> tags) {
super(context, 0, objects);
this.listTag = tags;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
... ....
}
}

我們來看看getView方法:
復(fù)制代碼 代碼如下:

//該方法根據(jù)adapter的順序一行一行的組織列表
//其中position表示第幾行,也就是當(dāng)前行在adapter的位置,
//convertView表示第幾行的View
View getView(int position, View convertView, ViewGroup parent);

現(xiàn)在我們就是要重寫getView方法,來實(shí)現(xiàn)列表中嵌入分組標(biāo)簽。
分組標(biāo)簽也是列表數(shù)據(jù)項(xiàng)之一,也是被一行一行的畫上去的,但是它和其他數(shù)據(jù)項(xiàng)UI是不一致的,所以我們需要準(zhǔn)備2套數(shù)據(jù)項(xiàng)布局模板:
數(shù)據(jù)項(xiàng)模板group_list_item.xml:
復(fù)制代碼 代碼如下:

<?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/list_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<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"/>
</LinearLayout>

標(biāo)簽項(xiàng)模板group_list_item_tag.xml:
復(fù)制代碼 代碼如下:

<!-- 只有文字,但是高度小店,背景色設(shè)置為555555灰色 -->
<?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"/>
</LinearLayout>

好,我們現(xiàn)在把這兩個模板應(yīng)用到getView方法中去:
復(fù)制代碼 代碼如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
//根據(jù)標(biāo)簽類型加載不通的布局模板
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);
}
//顯示名稱
TextView textView = (TextView) view.findViewById(R.id.group_list_item_text);
textView.setText(getItem(position));
//返回重寫的view
return view;
}

4.禁止標(biāo)簽項(xiàng)的響應(yīng)事件:
在ArrayAdapter的父類BaseAdapter中提供了isEnable的()方法,我們看看這個方法:
復(fù)制代碼 代碼如下:

//默認(rèn)情況,如果這個方法不是分割符,返回true
//分隔符是無選中和無點(diǎn)擊事件的
//說白了,你想不想把改position項(xiàng)當(dāng)做分隔符,想的話就返回false,否則返回true
public boolean isEnabled (int position)

這個方法剛好用來禁用標(biāo)簽項(xiàng)的響應(yīng)事件。具體實(shí)現(xiàn)如下:
復(fù)制代碼 代碼如下:

@Override
public boolean isEnabled(int position) {
if(listTag.contains(getItem(position))){
return false;
}
return super.isEnabled(position);
}

現(xiàn)在標(biāo)簽項(xiàng)不會再有任何觸控效果了,猶如一塊死木板。
5.完整代碼:
整個Activity和Adapter代碼如下:
復(fù)制代碼 代碼如下:

public class GroupListActivity extends Activity {
private GroupListAdapter adapter = null;
private ListView listView = null;
private List<String> list = new ArrayList<String>();
private List<String> listTag = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group_list_activity);
setData();
adapter = new GroupListAdapter(this, list, listTag);
listView = (ListView)findViewById(R.id.group_list);
listView.setAdapter(adapter);
}
public void setData(){
list.add("A");
listTag.add("A");
for(int i=0;i<3;i++){
list.add("阿凡達(dá)"+i);
}
list.add("B");
listTag.add("B");
for(int i=0;i<3;i++){
list.add("比特風(fēng)暴"+i);
}
list.add("C");
listTag.add("C");
for(int i=0;i<30;i++){
list.add("查理風(fēng)云"+i);
}
}
private static class GroupListAdapter extends ArrayAdapter<String>{
private List<String> listTag = null;
public GroupListAdapter(Context context, List<String> objects, List<String> tags) {
super(context, 0, objects);
this.listTag = tags;
}
@Override
public boolean isEnabled(int position) {
if(listTag.contains(getItem(position))){
return false;
}
return super.isEnabled(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(listTag.contains(getItem(position))){
view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_tag, null);
}else{ 
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));
return view;
}
}
}

6.最終效果:


  • 嗶哩嗶哩Android項(xiàng)目編譯優(yōu)化

    嗶哩嗶哩Android項(xiàng)目編譯優(yōu)化

    這篇文章主要為大家介紹了嗶哩嗶哩Android項(xiàng)目編譯優(yōu)化詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android網(wǎng)絡(luò)連接判斷與相關(guān)處理

    Android網(wǎng)絡(luò)連接判斷與相關(guān)處理

    這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)連接判斷操作,幫助大家判斷WIFI網(wǎng)絡(luò)是否可用,判斷MOBILE網(wǎng)絡(luò)是否可用,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android封裝高德地圖定位工具類Util的詳細(xì)步驟

    Android封裝高德地圖定位工具類Util的詳細(xì)步驟

    這篇文章主要給大家介紹了關(guān)于Android封裝高德地圖定位工具類Util的相關(guān)資料,封裝成工具類后非常方便以后的項(xiàng)目,可以直接使用,文中也給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下
    2021-07-07
  • Android自定義View實(shí)現(xiàn)多邊形統(tǒng)計(jì)圖示例代碼

    Android自定義View實(shí)現(xiàn)多邊形統(tǒng)計(jì)圖示例代碼

    這篇文章主要給大家介紹了關(guān)于Android自定義View如何實(shí)現(xiàn)多邊形統(tǒng)計(jì)圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • 輕松實(shí)現(xiàn)功能強(qiáng)大的Android刮獎效果控件(ScratchView)

    輕松實(shí)現(xiàn)功能強(qiáng)大的Android刮獎效果控件(ScratchView)

    這篇文章主要為大家詳細(xì)介紹了ScratchView如何一步步打造萬能的Android刮獎效果控件,,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android開發(fā)筆記 最好使用eclipse

    Android開發(fā)筆記 最好使用eclipse

    值得注意一點(diǎn)的是,雖然Myeclipse比eclipse功能更強(qiáng)大,但是在具體的安卓開發(fā)過程當(dāng)中,最好還是選用eclipse,sdk跟eclipse的兼容性更好
    2012-11-11
  • Android compose氣泡升起和水滴下墜動畫實(shí)現(xiàn)示例

    Android compose氣泡升起和水滴下墜動畫實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了Android compose氣泡升起和水滴下墜動畫實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • 詳解Android業(yè)務(wù)組件化之URL Schema使用

    詳解Android業(yè)務(wù)組件化之URL Schema使用

    這篇文章主要為大家詳細(xì)介紹了Android業(yè)務(wù)組件化之URL Schema使用,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 最新評論