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

自定義Adapter并通過布局泵LayoutInflater抓取layout模板編輯每一個item實(shí)現(xiàn)思路

 更新時間:2013年06月07日 17:26:43   作者:  
自定義Adapter并通過布局泵LayoutInflater抓取layout模板編輯每一個item,下面我們開始學(xué)習(xí)這一篇的內(nèi)容,感興趣的朋友可以了解下哈
寫在前面的話:
看到標(biāo)題這么長可能大家有點(diǎn)抓狂了,是的,我在剛剛學(xué)這一篇的時候有一些不理解,什么是布局泵?編輯每一個模板然后什么是自定義Adapter?下面我們開始學(xué)習(xí)這一篇的內(nèi)容

首選上一張圖,實(shí)現(xiàn)效果:
 

邏輯解析:
首先上面這個圖是最終的實(shí)現(xiàn)效果了,有點(diǎn)像我們的通訊錄聯(lián)系人的排版方式,說一下layout的布局吧。很簡單,其實(shí)就是一個ListView組件。但是這個ListView組件用的Adapter有一點(diǎn)不同。我們自己定義了一個adapter并且通過getview方法對每一個條目進(jìn)行了編輯和排版。然后最后將我們自定義的Adapter放入到了我們的ListView中以實(shí)現(xiàn)展示了這種效果下面我給出這個實(shí)現(xiàn)的重要代碼片段,然后加以分析

代碼分析:
第一步:理解全局變量
復(fù)制代碼 代碼如下:

/****
* 其中l(wèi)isttag是分類的分割標(biāo)簽,每個組的head
*/
private List<String> list = null; //存放聯(lián)系人數(shù)據(jù)的list
private List<String> listtag = null; //存放字母的數(shù)據(jù)的list
private GroupListAdapter adapter = null; //自定義的Adapter對象
private ListView listView = null; //主layout中用到的listview

第二步:MainActivity的onCreate方法處理
復(fù)制代碼 代碼如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setData(); //初始化聯(lián)系人和首字母的數(shù)據(jù)
adapter = new GroupListAdapter(this, list, listtag); //[重要],將每一個item重寫排版和編輯得到信息view之后放到adapter里面
//將我們自定義的adapter放到listview里面
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
}

說明:注釋中寫的很清楚,我們總共就做了這幾個邏輯處理,看到這里還不夠清楚,帶著我們的疑問往下看一步步的就清楚了。

第三步:初始化list數(shù)據(jù)(比較簡單)
復(fù)制代碼 代碼如下:

//插入要顯示的數(shù)據(jù)。listtag是聯(lián)系人上面的分組ABCD。list為聯(lián)系人數(shù)據(jù)
public void setData() {
list = new ArrayList<String>();
listtag = new ArrayList<String>();
list.add("A");
listtag.add("A");
for (int i = 0; i < 4; i++) {
list.add("阿波次的" + i);
}
list.add("B");
listtag.add("B");
for (int i = 0; i < 4; i++) {
list.add("波士頓" + i);
}
list.add("C");
listtag.add("C");
for (int i = 0; i < 4; i++) {
list.add("車轍" + i);
}
}

第四步:自定義Adapter(重要)
復(fù)制代碼 代碼如下:

//自定義listAdapter,利用布局泵的方式定義每一個listview條目
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;
}

//禁用標(biāo)簽項(xiàng)的選擇事件
@Override
public boolean isEnabled(int position) {
if (listTag.contains(getItem(position))) {
return false;
}
return super.isEnabled(position);
}

//本方法是迭代的,迭代對象為構(gòu)造方法第二個對象,依次取出每一個list條目,(重寫就會執(zhí)行)
@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;
}
}

說明:
OK,第四步可以說是我們整個功能的核心部分了,注意我們是繼承了一個ArrayAdapter,然后重寫了兩個方法。首先注意這兩個方法的作用,注釋上給了很清楚,如果大家看不懂可以刪除掉方法然后跑一跑程序加深一下理解。就明白這兩個方法是做什么的了。注意:重寫的方法一旦重寫就肯定會執(zhí)行,
單獨(dú)說一下getview方法吧。你可以想象一下放個方法在我們定義的list對象的for循環(huán)迭代里面然后就存在了下面的關(guān)系
Position = I;
View = listview中應(yīng)用的每一個viewitem
既然這樣我想大家應(yīng)該很容易看明白了。在迭代的同時利用當(dāng)前的position對應(yīng)的item和listTag對象去做對比。如果存在在這個其中就說明是標(biāo)題行,那么就用布局泵拿到標(biāo)題行對應(yīng)的layout里面的view然后編輯該view為對應(yīng)的方式。不存在就是普通的聯(lián)系人行。這么說您懂了嗎?
在經(jīng)過了上面的這個處理之后我們再將每一個行view放到adapter中去。然后形成了我們最終的效果
Layout文件:
主layout:
1、activity_main.xml文件
復(fù)制代碼 代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
>
</ListView>
</LinearLayout>

模板layout文件:
1、 group_list_item_tag.xml文件 [聯(lián)系人layout模板]
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 聯(lián)系人layout模板 -->
<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>

2、 group_list_item.xml 文件標(biāo)題行l(wèi)ayout模板
復(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/ic_launcher"
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>

最后的說明:
后面的2個模板只是對應(yīng)listview的每一個item的。希望大家理解。
源碼下載

相關(guān)文章

最新評論