ExpandableListView實現(xiàn)手風琴效果
本文實例為大家分享了ExpandableListView實現(xiàn)手風琴效果的具體代碼,供大家參考,具體內容如下
1. 效果示例圖
2. 創(chuàng)建方法
(1)第一種方法與ListView等普通控件一樣,直接在布局文件中添加ExpandableListView控件即可。
(2)第二種方法則是創(chuàng)建一個Activity繼承自ExpandableListActivity,而后通過getExpandableListView()方法可獲得一個ExpandableListView對象。
第二種方法僅適用于一個頁面中只有一個ExpandableListView的情況。繼承的Activity不需要再調用setContentView()方法,在ExpandableListActivity中已經關聯(lián)了一個系統(tǒng)定義的布局文件。
3. 部分屬性和點擊事件
android:groupIndicator、android:childIndicator:組條目和子條目前面的圖標,默認值為箭頭,可設置自定義圖片資源。若不顯示該圖標,則設置為@null。
android:divider、android:childDivider:組和子條目的分隔線。
ExpandableListView的點擊事件有兩個,分別對應組和子條目的點擊事件:
設置組的點擊事件:setOnGroupClickListener(OnGroupClickListener listener)
設置子條目的點擊事件:setOnChildClickListener(OnChildClickListener listener)
5. 適配器
根據數(shù)據源的不同,可使用的適配器有兩個:BaseExpandableListAdapter和CursorTreeAdapter,其中,CursorTreeAdapter用于數(shù)據源為Cursor對象的情況下,其它情況則使用BaseExpandableListAdapter。
(1)BaseExpandableListAdapter需要重寫的方法:
getGroup():從數(shù)據源中獲取組的數(shù)據內容。
getGroupCount():獲取組的總數(shù)。
getGroupId():獲取組的ID。
getGroupView():獲取組的視圖。
getChild():從數(shù)據源中獲取子條目的內容。
getChildCount():獲取指定組中的子條目總數(shù),并非全部的子條目。
getChildId():獲取子條目的ID。
getChildView():獲取子條目的視圖
hasStableIds():判斷id對應的條目是否已經繪制,用于優(yōu)化列表。
isChildSelectable():子條目是否允許點擊,若返回false,則子條目點擊事件無效。
(2)CursorTreeAdapter需要重寫的方法:
CursorTreeAdapter():構造方法傳入組的Cursor對象。
getChildrenCursor():傳入組的Cursor對象,獲取相應的組的子條目的Cursor對象。
newGroupView():創(chuàng)建組的視圖,返回一個新的視圖。
bindGroupView():在這里綁定組視圖的數(shù)據內容,第一個參數(shù)即newGroupView()方法的返回值。
newChildView():創(chuàng)建子條目的視圖。
bindChildView():綁定子條目視圖的數(shù)據內容。
6. 簡單范例
實現(xiàn)效果圖中的例子。
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.studying.expandablelistviewdemo.MainActivity"> <ExpandableListView android:id="@+id/elv_local_data" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
Activity:
public class MainActivity extends Activity { private ExpandableListView elv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); elv = (ExpandableListView) findViewById(R.id.elv_local_data); MyBaseExpandableListAdapter adapter = new MyBaseExpandableListAdapter(this, LoadData.getGroupData(), LoadData.getChildData()); elv.setAdapter(adapter); } }
加載測試數(shù)據用的工具類:
public class LoadData { // 組的數(shù)據內容 public static List<String> getGroupData() { List<String> groupDataList = new ArrayList<>(); groupDataList.add("計算機基礎"); groupDataList.add("安卓開發(fā)"); return groupDataList; } // 子條目的數(shù)據內容 public static List<List<String>> getChildData() { List<List<String>> childDataList = new ArrayList<>(); List<String> group1 = new ArrayList<>(); group1.add("數(shù)據結構"); group1.add("算法"); group1.add("計算機網絡"); childDataList.add(group1); List<String> group2 = new ArrayList<>(); group2.add("控件使用"); group2.add("網絡操作"); group2.add("數(shù)據存儲"); group2.add("四大組件"); childDataList.add(group2); return childDataList; } }
適配器:
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private List<String> groupName; private List<List<String>> childName; public MyBaseExpandableListAdapter(Context mContext, List<String> groupName, List<List<String>> childName) { this.mContext = mContext; this.groupName = groupName; this.childName = childName; } @Override public int getGroupCount() { return groupName.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public String getGroup(int groupPosition) { return groupName.get(groupPosition); } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { convertView = View.inflate(mContext, R.layout.item_group_name, null); TextView groupName = (TextView) convertView.findViewById(R.id.group_name); groupName.setText(getGroup(groupPosition)); return convertView; } @Override public int getChildrenCount(int groupPosition) { return childName.get(groupPosition).size(); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public String getChild(int groupPosition, int childPosition) { return childName.get(groupPosition).get(childPosition); } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { convertView = View.inflate(mContext, R.layout.item_child_name, null); TextView childName = (TextView) convertView.findViewById(R.id.child_name); childName.setText(getChild(groupPosition, childPosition)); return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android之帶group指示器的ExpandableListView(自寫)
- Android ExpandableListView展開列表控件使用實例
- Android ExpandableListView長按事件的完美解決辦法
- Android之IphoneTreeView帶組指示器的ExpandableListView效果
- Android UI控件ExpandableListView基本用法詳解
- Android中ExpandableListView的用法實例
- Android 關于ExpandableListView刷新問題的解決方法
- android使用ExpandableListView控件實現(xiàn)小說目錄效果的例子
- Android改變ExpandableListView的indicator圖標實現(xiàn)方法
- Android 關于ExpandableListView去掉里頭分割線的方法
相關文章
AndroidStudio安全管理簽名文件keystroe和簽名密碼(星空武哥)
我們在使用AndroidStudio進行release版的apk簽名的時候,往往都是將簽名文件keystore放在項目中,密碼寫在build.gradle中,keystore和密碼就隨著代碼上傳到了Git倉庫中了,這樣往往很不安全,因為這樣被人獲取2017-09-09unity5.6 導出gradle工程 Android Studio 導入問題及處理方法
這篇文章主要介紹了unity5.6 導出gradle工程 Android Studio 導入問題及處理方法,需要的朋友可以參考下2017-12-12Android使用Notification實現(xiàn)通知功能
這篇文章主要為大家詳細介紹了Android使用Notification實現(xiàn)通知功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11Android頭像上傳功能的實現(xiàn)代碼(獲取頭像加剪切)
最近在做一個頭像上傳的項目,下面小編給大家分享Android頭像上傳功能的實現(xiàn)代碼,需要的的朋友參考下吧2017-08-08Android同步異步任務與多線程及Handler消息處理機制基礎詳細講解
這篇文章主要介紹了Android同步異步任務與多線程及Handler消息處理機制基礎,handler其實就是主線程在起了一個子線程,子線程運行并生成Message,Looper獲取message并傳遞給Handler,Handler逐個獲取子線程中的Message,感興趣的小伙伴快跟隨小編一起學習一下2022-11-11Android開發(fā)的IDE、ADT、SDK、JDK、NDK等名詞解釋
這篇文章主要介紹了Android開發(fā)的IDE、ADT、SDK、JDK、NDK等名詞解釋,對這些概念搞不清楚是一件痛苦的事,本文就簡潔講解了這些名詞的含義,一起掃盲吧,需要的朋友可以參考下2015-06-06