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

1.樣本數(shù)據(jù):
為了突出重點(diǎn),直擊要點(diǎn),這里提供一個(gè)整理好的數(shù)據(jù)樣本:
- //list:數(shù)據(jù)集合
- private List
list = new ArrayList (); - //listTag:Tag集合,其中Tag是分類的分割標(biāo)簽,每個(gè)分組的header
- private List
listTag = new ArrayList (); - 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)備:
放置一個(gè)listView來呈現(xiàn)數(shù)據(jù)。
group_list_activity.xml:
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:cacheColorHint="#00000000"/>
3.自定義Adapter(本文繼承ArrayAdapter):
這個(gè)是本文的重點(diǎn)和核心。
Adapter接口為數(shù)據(jù)和界面搭建了一個(gè)訪問的橋梁,最重要的就是getView()方法,用這個(gè)方法我們可以實(shí)現(xiàn)一定程度的界面自定義。
ArrayAdapter間接實(shí)現(xiàn)了Adapter接口,這里我們簡(jiǎn)單起見,數(shù)據(jù)源只是提供單一的String數(shù)組。
- private static class GroupListAdapter extends ArrayAdapter
{ - //存放標(biāo)簽的列表,用來判斷數(shù)據(jù)項(xiàng)的類型
- //如果數(shù)據(jù)項(xiàng)在標(biāo)簽列表中,則是標(biāo)簽項(xiàng),否則是數(shù)據(jù)項(xiàng)
- private List
listTag = null; - public GroupListAdapter(Context context, List
objects, List tags) { - super(context, 0, objects);
- this.listTag = tags;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ... ....
- }
- }
我們來看看getView方法:
- //該方法根據(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:
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:padding="5dip">
- android:src="@drawable/list_icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- android:id="@+id/group_list_item_text"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:paddingLeft="5dip"
- android:gravity="center_vertical"/>
標(biāo)簽項(xiàng)模板group_list_item_tag.xml:
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#555555"
- android:paddingLeft="10dip">
- android:id="@+id/group_list_item_text"
- android:layout_width="wrap_content"
- android:layout_height="20dip"
- android:textColor="#ffffff"
- android:gravity="center_vertical"/>
好,我們現(xiàn)在把這兩個(gè)模板應(yīng)用到getView方法中去:
- @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的()方法,我們看看這個(gè)方法:
- //默認(rèn)情況,如果這個(gè)方法不是分割符,返回true
- //分隔符是無選中和無點(diǎn)擊事件的
- //說白了,你想不想把改position項(xiàng)當(dāng)做分隔符,想的話就返回false,否則返回true
- public boolean isEnabled (int position)
這個(gè)方法剛好用來禁用標(biāo)簽項(xiàng)的響應(yīng)事件。具體實(shí)現(xiàn)如下:
- @Override
- public boolean isEnabled(int position) {
- if(listTag.contains(getItem(position))){
- return false;
- }
- return super.isEnabled(position);
- }
現(xiàn)在標(biāo)簽項(xiàng)不會(huì)再有任何觸控效果了,猶如一塊死木板。
5.完整代碼:
整個(gè)Activity和Adapter代碼如下:
- public class GroupListActivity extends Activity {
- private GroupListAdapter adapter = null;
- private ListView listView = null;
- private List
list = new ArrayList (); - private List
listTag = new ArrayList (); - @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
{ - private List
listTag = null; - public GroupListAdapter(Context context, List
objects, List 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.最終效果:
網(wǎng)頁(yè)標(biāo)題:Android超炫特效:ListViewitem拖拽效果(下)
分享網(wǎng)址:http://m.fisionsoft.com.cn/article/dhiccid.html


咨詢
建站咨詢
