android控制項開發之ExpandableListActivity(一),android自訂控制項

來源:互聯網
上載者:User

android控制項開發之ExpandableListActivity(一),android自訂控制項
android控制項開發之ExpandableListActivity本博文主要講述的是android控制項開發中的ExpandableListActivity(可擴充List)控制項的簡單用法。
java代碼:
package com.example.expandablelistactivity;


import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.ExpandableListView.OnChildClickListener;




/*建立一個ExpandableListActivity需要如下步驟:
 * 1、首先需要設定ExpandableListView的配置樣式
 * 2、建立一個一級條目的配置樣式檔案
 * 3、建立一個二級條目的配置樣式檔案
 * 4、設定一級條目的資料
 * 5、設定二級條目的資料
 * 6、使用SimpleExpandableListAdapter為ExpandableListActivity提供資料
 * 7、設定監聽器
 * */


public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//建立一級條目的對象
ArrayList<HashMap<String, String>> groups = new ArrayList<HashMap<String,String>>();
HashMap<String, String> group1 = new HashMap<String, String>();
HashMap<String, String> group2 = new HashMap<String, String>();

//給一級條目設定資料
group1.put("group", "group1");
group2.put("group", "group2");

//將資料添加到一級條目的對象中
groups.add(group1);
groups.add(group2);


//建立二級條目的child1對象。用來給第一個一級條目提供資料
ArrayList<HashMap<String, String>> child1 = new ArrayList<HashMap<String,String>>();
HashMap<String, String> child1data1 = new HashMap<String, String>();
HashMap<String, String> child1data2 = new HashMap<String, String>();
HashMap<String, String> child1data3 = new HashMap<String, String>();

child1data1.put("child", "child1data1");
child1data2.put("child", "child1data2");
child1data3.put("child", "child1data3");

child1.add(child1data1);
child1.add(child1data2);
child1.add(child1data3);


//建立二級條目的child2對象。用來給第二個一級條目提供資料
ArrayList<HashMap<String, String>> child2 = new ArrayList<HashMap<String,String>>();
HashMap<String, String> child2data1 = new HashMap<String, String>();
child2data1.put("child", "child2data1");
child2.add(child2data1);


//建立二級條目的對象。
ArrayList<ArrayList<HashMap<String, String>>> childs = new ArrayList<ArrayList<HashMap<String,String>>>();

childs.add(child1);
childs.add(child2);


SimpleExpandableListAdapter ela = new SimpleExpandableListAdapter(this, groups,
R.layout.group, new String[]{"group"}, new int[]{R.id.groupText}, 
childs, R.layout.child, new String[]{"child"}, new int[]{R.id.childText});

//給ExpandableListView設定SimpleExpandableListAdapter適配器
ExpandableListView expandableListView = (ExpandableListView)findViewById(android.R.id.list);
expandableListView.setAdapter(ela);


//綁定ExpandableListView child的監聽器
expandableListView.setOnChildClickListener(new expandableChildLisenter());

}

//實現二級菜單監聽器
class expandableChildLisenter implements OnChildClickListener{
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
System.out.println("group = " + groupPosition);
System.out.println("child = " + childPosition);
System.out.println("id = " + id);

return false;
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

主布局檔案main.xml代碼:此布局檔案主要是定義一個ExpandableListView
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <!--設定ExpandableListView主介面的布局檔案  -->
    <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" 
        android:layout_below="@id/myText"
        ></ExpandableListView>


</RelativeLayout>



一級列表布局檔案group.xml代碼:此布局檔案主要設定的是一級列表的樣式<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!--主要是設定一級條目的樣式  -->
    <TextView 
        android:id="@+id/groupText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="30dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textSize="20sp"/>


</LinearLayout>

效果如下:


二級列表布局檔案child.xml代碼:此布局檔案主要是用來設定二級列表的樣式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <!--設定二級條目的樣式  -->
    <TextView 
        android:id="@+id/childText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="35dp"
        android:textSize="15sp"/>

</LinearLayout>

效果如下:

當點擊二級列表中的某項時,會在LogCat中輸出當前點擊的菜單的一級列表POS,二級列表POS,以及二級列表的ID,如下:




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.