0207_android中的基本控件下020719樹型組件_第1頁
0207_android中的基本控件下020719樹型組件_第2頁
0207_android中的基本控件下020719樹型組件_第3頁
0207_android中的基本控件下020719樹型組件_第4頁
0207_android中的基本控件下020719樹型組件_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Android開發(fā)實(shí)戰(zhàn)經(jīng)典第7章: Android中的基本控件(下) 樹型組件:ExpandableListView北京魔樂科技軟件學(xué)院李興華研發(fā)部本章目標(biāo)掌握樹型組件的定義;可以使用對(duì)樹操作進(jìn)行。E-MAIL:樹型組件:ExpandableListView ListView組件可以為用戶提供列表的顯示功能,但是如果要想對(duì)這些列表數(shù)據(jù)進(jìn)行分組的管理,則就需要使用android.widget.ExpandableListView 組件完成,ExpandableListView類的繼承結(jié)構(gòu)如下:java.lang.Object android.view.View andr

2、oid.view.ViewGroup android.widget.AdapterView android.widget.AbsListView android.widget.ListView android.widget.ExpandableListViewE-MAIL:ExpandableListView類的常用操作方法(A)E-MAIL:No.方法類型描述1public ExpandableListView (Context context)構(gòu)造實(shí)例化ExpandableListView類的對(duì)象2public boolean colla

3、pseGroup(int groupPos)普通關(guān)閉指定的分組3public boolean expandGroup(int groupPos)普通打開指定的分組4public ListAdapter getAdapter()普通取得保存數(shù)據(jù)的ListAdapter對(duì)象5public ExpandableListAdapter getExpandableListAdapter()普通取得保存數(shù)據(jù)的ExpandableListAdapter對(duì)象6public static int getPackedPositionType(longpackedPosition)普通取得操作的菜單項(xiàng)的類型(判斷是

4、菜單組,還是菜單項(xiàng))7public static int getPackedPositionGroup(longpackedPosition)普通取得操作的所在的菜單組編號(hào)8public static int getPackedPositionChild(longpackedPosition)普通取得操作所在的菜單項(xiàng)編號(hào)9public long getSelectedId()普通取得當(dāng)前所操作的菜單ID,如果沒有則返回-110public void setAdapter(ExpandableListAdapter adapter)普通設(shè)置適配器數(shù)據(jù)對(duì)象ExpandableListView類的常

5、用操作方法(B)E-MAIL:11public void setAdapter(ListAdapter adapter)普通設(shè)置適配器數(shù)據(jù)對(duì)象12public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup)普通設(shè)置選中的菜單項(xiàng)13public void setSelectedGroup(int groupPosition)普通設(shè)置選中的菜單組14public void setOnChildClickListener (Expandable

6、ListView.OnChildClickListeneronChildClickListener)普通設(shè)置菜單組的單擊處理15public void setOnGroupClickListener (ExpandableListView.OnGroupClickListeneronGroupClickListener)普通設(shè)置菜單組的單擊處理16public void setOnGroupCollapseListener (ExpandableListView.OnGroupCollapseListeneronGroupCollapseListener)普通設(shè)置菜單組關(guān)閉的處理17publi

7、c void setOnGroupExpandListener (ExpandableListView.OnGroupExpandListeneronGroupExpandListener)普通設(shè)置菜單組打開的處理18public void setOnItemClickListener(AdapterView.OnItemClickListener l)普通設(shè)置選項(xiàng)單擊的處理ExpandableListView提供的接口E-MAIL:No.接口名稱作用定義方法1ExpandableListView. OnChildClickListener單擊子選項(xiàng)public bo

8、olean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id)2ExpandableListView. OnGroupClickListener單擊分組項(xiàng)public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)3ExpandableListView.OnGroupCollapseListener分組關(guān)閉public void onGrou

9、pCollapse(int groupPosition)4ExpandableListView.OnGroupExpandListener分組打開public void onGroupExpand(int groupPosition)BaseExpandableListAdapter要想進(jìn)行數(shù)據(jù)顯示的設(shè)置也需要一個(gè)適配器類,此類繼承android.widget.BaseExpandableListAdapter類 ,E-MAIL:BaseExpandableListAdapter的子類所需要覆寫的方法E-MAIL:No.方法類型描述1publ

10、ic Object getChild(int groupPosition, int childPosition)普通獲得指定組中的指定索引的子項(xiàng)數(shù)據(jù)2public long getChildId(int groupPosition, int childPosition)普通獲得指定子項(xiàng)數(shù)據(jù)的ID3public View getChildView(int groupPosition, intchildPosition,boolean isLastChild, ViewconvertView, ViewGroup parent)普通獲得指定子項(xiàng)的View組件4public int getChild

11、renCount(int groupPosition)普通取得指定組中所有子項(xiàng)的個(gè)數(shù)5public Object getGroup(int groupPosition)普通取得指定組數(shù)據(jù)6public int getGroupCount()普通取得所有組的個(gè)數(shù)7public long getGroupId(int groupPosition)普通取得指定索引的組ID8public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)普通獲得指定組的View組件9pu

12、blic boolean hasStableIds()普通如果返回true,表示子項(xiàng)和組的ID始終表示一個(gè)固定的組件對(duì)象10public boolean isChildSelectable(int groupPosition, intchildPosition)普通判斷指定的子選項(xiàng)是否被選中范例:定義樹型組件E-MAIL:范例:定義適配器類 MyExpandableListAdapter.java(A)E-MAIL:package org.lxh.demo;import android.content.Context; import andr

13、oid.view.Gravity; import android.view.View; import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;/ 繼承BaseExpandableListAdapter并覆寫類中的抽象方法public class MyExpandableListAdapter extends BaseExpandableListAdapter pub

14、lic String groups = 我的好友, 家人, 同事, 黑 ;/ 組名稱public String children = 李興華, 董鳴楠, 王月清, 周艷軍 , 父親, 母親 , 劉宏偉, 李祺, 劉媛 , 票販子, 商 ;/ 定義組項(xiàng)private Context context = null;/ 保存上下文對(duì)象public MyExpandableListAdapter(Context context) / 構(gòu)造方法接收this.context = context;Overridepublic Object getChild(int groupPosition, int ch

15、ildPosition) / 取得指定的子項(xiàng)return this.childrengroupPositionchildPosition;Overridepublic long getChildId(int groupPosition, int childPosition) / 取得子項(xiàng)IDreturn childPosition;范例:定義適配器類 MyExpandableListAdapter.java(B)E-MAIL:public TextView buildTextView() / 自定義方法,建立文本AbsListView.LayoutParams par

16、am = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 35);/ 指定布局參數(shù)TextView textView = new TextView(this.context);/ 創(chuàng) 建 TextView textView.setLayoutParams(param);/ 設(shè)置布局參數(shù)textView.setTextSize(15.0f);/ 設(shè)置文字大小textView.setGravity(Gravity.LEFT);/ 左對(duì)齊textView.setPadding(40, 8, 3, 3);/ 間距retu

17、rn textView;/ 返回組件Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) / 返回子項(xiàng)組件TextView textView = buildTextView();/ 創(chuàng) 建 TextView textView.setText(getChild(groupPosition,childPosition).toString();/ 設(shè)置顯示文字return textView;Overri

18、depublic int getChildrenCount(int groupPosition) / 取得子項(xiàng)個(gè)數(shù)return this.childrengroupPosition.length;/ 取得子項(xiàng)個(gè)數(shù)Overridepublic Object getGroup(int groupPosition) / 取得組對(duì)象return this.groupsgroupPosition;范例:定義適配器類 MyExpandableListAdapter.java(C)E-MAIL:Overridepublic int getGroupCount() / 取得組個(gè)數(shù)re

19、turn this.groups.length;Overridepublic long getGroupId(int groupPosition) / 取得組IDreturn groupPosition;Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) / 取得組顯示組件TextView textView = buildTextView();/ 建立組件textView.setText(this.getGroup(groupPosi

20、tion).toString();/ 設(shè)置文字return textView;Overridepublic boolean hasStableIds() return true;Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) return true;范例:定義布局管理器 main.xmlE-MAIL:布局管理器高度為屏幕高度 組件高度為自身高度范例:定義Activity程序,實(shí)現(xiàn)樹型列表(A)E-MAIL:package org.lx

21、h.demo;import android.app.Activity;import android.os.Bundle;import android.view.ContextMenu;import android.view.ContextMenu.ContextMenuInfo;import android.view.View;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildCl

22、ickListener; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.ExpandableListView.OnGroupCollapseListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.Toast;public class MyExpandableListViewDemo extends Activity private Exp

23、andableListView elistview = null;/ 定義樹型組件private ExpandableListAdapter adapter = null;/ 定義適配器對(duì)象Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); super.setContentView(R.layout.main);/ 默認(rèn)布局管理器this.elistview = (ExpandableListView) super.findViewById(R.id.elistv

24、iew);/ 取得組件this.adapter = new MyExpandableListAdapter(this);/ 實(shí)例化適配器this.elistview.setAdapter(this.adapter);/ 設(shè)置適配器super.registerForContextMenu(this.elistview);/ 注冊(cè)上下文菜單this.elistview.setOnChildClickListener(new OnChildClickListenerImpl(); / 設(shè)置子項(xiàng)單擊this.elistview.setOnGroupClickListener(new OnGroupCl

25、ickListenerImpl(); / 設(shè)置組項(xiàng)單擊this.elistview.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl(); / 關(guān)閉分組this.elistview.setOnGroupExpandListener(new OnGroupExpandListenerImpl();/ 展開分組范例:定義Activity程序,實(shí)現(xiàn)樹型列表(B)E-MAIL:private class OnChildClickListenerImpl implements OnChildClickListe

26、ner Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) Toast.makeText(MyExpandableListViewDemo.this,子選項(xiàng)被選中,groupPosition = + groupPosition+ ,childPosition = + childPosition,Toast.LENGTH_SHORT).show();/ 顯示提示框return false;private class

27、OnGroupClickListenerImpl implements OnGroupClickListener Overridepublic boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) Toast.makeText(MyExpandableListViewDemo.this,分組被選中,groupPosition = + groupPosition,Toast.LENGTH_SHORT).show();/ 顯示提示框return false;private class O

28、nGroupCollapseListenerImpl implementsOnGroupCollapseListener Overridepublic void onGroupCollapse(int groupPosition) Toast.makeText(MyExpandableListViewDemo.this,關(guān)閉分組,groupPosition = + groupPosition, Toast.LENGTH_SHORT).show();/ 顯示提示框范例:定義Activity程序,實(shí)現(xiàn)樹型列表(C)E-MAIL:private class OnGroupExpandListenerImpl implements OnGroupExpandListener Overr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論