ExpandableListView實現手風琴效果

来源:http://www.cnblogs.com/joahyau/archive/2017/08/23/7420820.html
-Advertisement-
Play Games

1. 效果示例圖 2. 創建方法 (1)第一種方法與ListView等普通控制項一樣,直接在佈局文件中添加ExpandableListView控制項即可。 (2)第二種方法則是創建一個Activity繼承自ExpandableListActivity,而後通過getExpandableListView( ...


1. 效果示例圖

ExpandableListView

ExpandableListView

ExpandableListView

2. 創建方法

(1)第一種方法與ListView等普通控制項一樣,直接在佈局文件中添加ExpandableListView控制項即可。

(2)第二種方法則是創建一個Activity繼承自ExpandableListActivity,而後通過getExpandableListView()方法可獲得一個ExpandableListView對象。

第二種方法僅適用於一個頁面中只有一個ExpandableListView的情況。繼承的Activity不需要再調用setContentView()方法,在ExpandableListActivity中已經關聯了一個系統定義的佈局文件。

3. 部分屬性和點擊事件

android:groupIndicator、android:childIndicator:組條目和子條目前面的圖標,預設值為箭頭,可設置自定義圖片資源。若不顯示該圖標,則設置為@null

android:divider、android:childDivider:組和子條目的分隔線。

ExpandableListView的點擊事件有兩個,分別對應組和子條目的點擊事件:

設置組的點擊事件:setOnGroupClickListener(OnGroupClickListener listener)

設置子條目的點擊事件:setOnChildClickListener(OnChildClickListener listener)

5. 適配器

根據數據源的不同,可使用的適配器有兩個:BaseExpandableListAdapter和CursorTreeAdapter,其中,CursorTreeAdapter用於數據源為Cursor對象的情況下,其它情況則使用BaseExpandableListAdapter。

(1)BaseExpandableListAdapter需要重寫的方法:

getGroup():從數據源中獲取組的數據內容。

getGroupCount():獲取組的總數。

getGroupId():獲取組的ID。

getGroupView():獲取組的視圖。

getChild():從數據源中獲取子條目的內容。

getChildCount():獲取指定組中的子條目總數,並非全部的子條目。

getChildId():獲取子條目的ID。

getChildView():獲取子條目的視圖

hasStableIds():判斷id對應的條目是否已經繪製,用於優化列表。

isChildSelectable():子條目是否允許點擊,若返回false,則子條目點擊事件無效。

(2)CursorTreeAdapter需要重寫的方法:

CursorTreeAdapter():構造方法傳入組的Cursor對象。

getChildrenCursor():傳入組的Cursor對象,獲取相應的組的子條目的Cursor對象。

newGroupView():創建組的視圖,返回一個新的視圖。

bindGroupView():在這裡綁定組視圖的數據內容,第一個參數即newGroupView()方法的返回值。

newChildView():創建子條目的視圖。

bindChildView():綁定子條目視圖的數據內容。

6. 簡單範例

實現效果圖中的例子。

佈局:

<?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);
    }
}

載入測試數據用的工具類:

public class LoadData {

    // 組的數據內容
    public static List<String> getGroupData() {
        List<String> groupDataList = new ArrayList<>();
        groupDataList.add("電腦基礎");
        groupDataList.add("安卓開發");
        return groupDataList;
    }

    // 子條目的數據內容
    public static List<List<String>> getChildData() {
        List<List<String>> childDataList = new ArrayList<>();

        List<String> group1 = new ArrayList<>();
        group1.add("數據結構");
        group1.add("演算法");
        group1.add("電腦網路");
        childDataList.add(group1);

        List<String> group2 = new ArrayList<>();
        group2.add("控制項使用");
        group2.add("網路操作");
        group2.add("數據存儲");
        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;
    }
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、jQuery基礎 Jquery它是一個庫(框架),要想使用它,必須先引入! jquery-1.8.3.js:一般用於學習階段。 jquery-1.8.3.min.js:用於項目使用階段 官網下載後,複製到當前WEB項目的js文件夾下,如下: 引入代碼:<script type="text/jav ...
  • [1]圖形變換 [2]矩陣變換 [3]全局陰影 [4]全局透明 [5]圖形合成 [6]裁剪區域 [7]圖像繪製 [8]使用圖像數據 [9]模式 [10]非零環繞 [11]交互 ...
  • Node.js之使用Buffer類處理二進位數據 Buffer類可以在處理TCP流或文件流時處理二進位數據,該類用來創建一個專門存放二進位數據的緩存區。 1. 創建Buffer對象 1.1 直接創建: bur = new BUffer(123) //123 為bur緩存區長度 1.2 初始化緩存區內 ...
  • 前端JS電商放大鏡效果: ...
  • 1.DRY——(Don't repeat yourself )儘量減少改動時要編輯的地方是代碼可維護的最大要素之一。 2.實例: 以上CSS樣式是對一個button進行樣式的定義,存在以下幾個問題: (1)當我們想讓按鈕更大時,可以改變font-size 屬性,通過改變字體來讓按鈕變大,但是相應的, ...
  • NSString *testvalue = @"back0 0x10Value"; if([testvalue rangeOfString:@"ck"].location !=NSNotFound) { NSLog(@"存在"); }else { NSLog(@"不存在"); } // ...
  • 說起線框圖工具,你腦海中浮現的是什麼呢?老字型大小Axure RP?還是設計新寵Mockplus?如今,形形色色的線框圖工具可以說是唾手可得,當然,這是一件好事,但是另一方面呢,過多的選擇也的確容易造成設計師的選擇困難。尤其是現在的行業形勢,快速的產品迭代和開發節奏,越來越少的時間成本,快速設計的需求與... ...
  • 文章轉自:http://msching.github.io/blog/2014/07/07/audio-in-ios/ 從事音樂相關的app開發也已經有一段時日了,在這過程中app的播放器幾經修改我也因此對於iOS下的音頻播放實現有了一定的研究。寫這個系列的博客目的一方面希望能夠拋磚引玉,另一方面也 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...