版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Android開發(fā)實(shí)戰(zhàn)經(jīng)典第7章: Android中的基本控件(下) 樹型組件:ExpandableListView北京魔樂科技軟件學(xué)院李興華研發(fā)部本章目標(biāo)掌握樹型組件的定義;可以使用對樹操作進(jìn)行。E-MAIL:樹型組件:ExpandableListView ListView組件可以為用戶提供列表的顯示功能,但是如果要想對這些列表數(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類的對象2public boolean colla
3、pseGroup(int groupPos)普通關(guān)閉指定的分組3public boolean expandGroup(int groupPos)普通打開指定的分組4public ListAdapter getAdapter()普通取得保存數(shù)據(jù)的ListAdapter對象5public ExpandableListAdapter getExpandableListAdapter()普通取得保存數(shù)據(jù)的ExpandableListAdapter對象6public static int getPackedPositionType(longpackedPosition)普通取得操作的菜單項(xiàng)的類型(判斷是
4、菜單組,還是菜單項(xiàng))7public static int getPackedPositionGroup(longpackedPosition)普通取得操作的所在的菜單組編號8public static int getPackedPositionChild(longpackedPosition)普通取得操作所在的菜單項(xiàng)編號9public long getSelectedId()普通取得當(dāng)前所操作的菜單ID,如果沒有則返回-110public void setAdapter(ExpandableListAdapter adapter)普通設(shè)置適配器數(shù)據(jù)對象ExpandableListView類的常
5、用操作方法(B)E-MAIL:11public void setAdapter(ListAdapter adapter)普通設(shè)置適配器數(shù)據(jù)對象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è)置也需要一個適配器類,此類繼承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)的個數(shù)5public Object getGroup(int groupPosition)普通取得指定組數(shù)據(jù)6public int getGroupCount()普通取得所有組的個數(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始終表示一個固定的組件對象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;/ 保存上下文對象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);/ 左對齊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)個數(shù)return this.childrengroupPosition.length;/ 取得子項(xiàng)個數(shù)Overridepublic Object getGroup(int groupPosition) / 取得組對象return this.groupsgroupPosition;范例:定義適配器類 MyExpandableListAdapter.java(C)E-MAIL:Overridepublic int getGroupCount() / 取得組個數(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;/ 定義適配器對象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);/ 注冊上下文菜單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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年下半年遂昌縣部分機(jī)關(guān)事業(yè)單位招聘編外用工備考題庫及一套答案詳解
- 2026年中國物流股份有限公司招聘備考題庫及完整答案詳解1套
- 2026年臨沂高新區(qū)公開招聘工作人員10人備考題庫完整參考答案詳解
- 2026年包頭鋼鐵(集團(tuán))有限責(zé)任公司招聘備考題庫及答案詳解1套
- 2026年中國船舶重工集團(tuán)大連船舶工業(yè)有限公司招聘備考題庫及1套完整答案詳解
- 2026年十五冶金建設(shè)集團(tuán)有限公司招聘等你來沖備考題庫及一套完整答案詳解
- 2026年東勝區(qū)消防安全服務(wù)中心專職工作人員招聘備考題庫及一套參考答案詳解
- 2026年成都理工大學(xué)工程技術(shù)學(xué)院招聘備考題庫及參考答案詳解
- 2026年國信證券股份有限公司福建分公司招聘備考題庫及完整答案詳解一套
- 2026年廣東粵運(yùn)交通股份有限公司招聘備考題庫參考答案詳解
- 河道工程測量施工方案
- 2025嵐圖汽車社會招聘參考題庫及答案解析(奪冠)
- 2025河南周口臨港開發(fā)區(qū)事業(yè)單位招才引智4人考試重點(diǎn)題庫及答案解析
- 2025年無人機(jī)資格證考試題庫+答案
- 南京工裝合同范本
- 登高作業(yè)監(jiān)理實(shí)施細(xì)則
- DB42-T 2462-2025 懸索橋索夾螺桿緊固力超聲拉拔法檢測技術(shù)規(guī)程
- 大學(xué)生擇業(yè)觀和創(chuàng)業(yè)觀
- 車載光通信技術(shù)發(fā)展及無源網(wǎng)絡(luò)應(yīng)用前景
- 工程倫理-形考任務(wù)四(權(quán)重20%)-國開(SX)-參考資料
- 初中書香閱讀社團(tuán)教案
評論
0/150
提交評論