android入門之ExpandableListView控制項的使用

來源:互聯網
上載者:User

本文通過學習ExpandableListView控制項的使用,同時學習ExpandableListActivity類和SimpleExpandableListAdapter適配器的使用。

通過例子講解,代碼中有詳細的注釋:

首先layout中有3個布局檔案:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ExpandableListView
     android:id="@id/android:list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:drawSelectorOnTop="true"
    /><!-- android:drawSelectorOnTop:是否高亮顯示 -->
 <TextView 
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:text="no data"
     android:id="@id/android:empty"
    />
</LinearLayout>

---------------------------------------------------------------------------

group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView 
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:text="no data"
     android:id="@+id/tvgroup"
     android:paddingBottom="10px"
     android:paddingLeft="50px"
     
    />
</LinearLayout>

--------------------------------------------------

child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
 <TextView 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="no data"
     android:id="@+id/tvchild"
     android:paddingLeft="50px"
    />
</LinearLayout>

---------------------------------------------------------------------

java代碼:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;

public class ExpandableListActivityTest extends ExpandableListActivity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
  Map<String, String> group1 = new HashMap<String, String>();
  group1.put("group", "huangweiyong");

  Map<String, String> group2 = new HashMap<String, String>();
  group2.put("group", "xiaoqi");

  Map<String, String> group3 = new HashMap<String, String>();
  group3.put("group", "hahaha");
  
  groups.add(group1);
  groups.add(group2);
  groups.add(group3);

  List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
  Map<String, String> child1Data1 = new HashMap<String, String>();
  child1Data1.put("child", "child1Data1");
  Map<String,String> child1Data2 = new HashMap<String,String>();
  child1Data2.put("child", "child1Data2");
  child1.add(child1Data1);
  child1.add(child1Data2);

  List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
  Map<String, String> child2Data1 = new HashMap<String, String>();
  child2Data1.put("child", "child1Data1");
  child2.add(child2Data1);
  
  List<Map<String, String>> child3 = new ArrayList<Map<String, String>>();
  Map<String, String> child3Data1 = new HashMap<String, String>();
  child3Data1.put("child", "child1Data1");
  child3.add(child3Data1);

  List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
  childs.add(child1);
  childs.add(child2);
  childs.add(child3);
  
  /**
   * 產生一個SimpleExpandableListAdapter對象
   * 1、context
   * 2、一級條目的資料
   * 3、用來設定一級條目樣式的布局檔案
   * 4、指定一級條目資料的key
   * 5、指定一級條目資料顯示的控制項id
   * 6、指定二級條目的資料
   * 7、用來設定二級條目樣式的布局檔案
   * 8、指定二級條目資料的key
   * 9、指定二級條目資料顯示的控制項id
   */
  SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
    this, groups, R.layout.group, new String[] { "group" },
    new int[] { R.id.tvgroup }, childs, R.layout.child,
    new String[] { "child" }, new int[] { R.id.tvchild });
  //將配接器物件設定給ExpandableListActivity
  setListAdapter(sela);
 }
}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.