看看今天實現的Android中個多級列表的功能,其實這就是一個小組件,但是如果用好了,可以實現很大的功能呢!接著,有點累,什麼都不說了,看看看實現的過程就可以了,挺簡單的!
1.看看布局文件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"
- android:background="@drawable/bg"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="那些上過與即將被上的大學課程:"
- android:textColor="#FF6100"
- android:textSize="20dp"
-
-
- />
- <ExpandableListView
- android:id="@+id/elistview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
-
-
- />
- </LinearLayout>
2.接著看看主活動mainActivity.java
- package com.wang;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.ContextMenu;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.widget.ExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.Toast;
- import android.widget.ExpandableListView.OnChildClickListener;
- import android.widget.ExpandableListView.OnGroupClickListener;
- import android.widget.ExpandableListView.OnGroupCollapseListener;
- import android.widget.ExpandableListView.OnGroupExpandListener;
-
- public class MaintActivity extends Activity {
- // 創建一個上下文菜單的方法
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
-
- super.onCreateContextMenu(menu, v, menuInfo);
-
- // 一個垂直滾動的兩級列表。取得菜單項
- ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
- // 獲得這個類型 的位置
- int type = ExpandableListView
- .getPackedPositionType(info.packedPosition);
- // 取得所在組的索引
- int group = ExpandableListView
- .getPackedPositionGroup(info.packedPosition);
- // 取得子菜單的索引
- int child = ExpandableListView
- .getPackedPositionGroup(info.packedPosition);
-
- Toast.makeText(MaintActivity.this,
- "類型 =" + type + " 分組:" + group + " 子選項:" + child,
- Toast.LENGTH_LONG).show();
-
- }
-
- private ExpandableListView eListView = null;
- private ExpandableListAdapter adapter = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
-
- // 去除標題,
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- // 取消狀態欄,充滿全屏
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.main);
-
- // 實例化組件
- this.eListView = (ExpandableListView) findViewById(R.id.elistview);
-
- // 聲明一個adapter對象
- adapter = new myExpendableadapler(this);
- // 設置適配器提供了數據
- this.eListView.setAdapter(this.adapter);
-
- // 注冊一個上下文菜單顯示給定的視圖(多個視圖可以顯示上下文菜單)。
- super.registerForContextMenu(this.eListView);
-
- // 設置點擊時候觸發的事件 1,子選項點擊事件 2。父選項單擊事件 3.分組打開事件 4.分組關閉事件
- this.eListView.setOnChildClickListener(new ChildClickListener());
- this.eListView.setOnGroupClickListener(new GroupClickListener());
- this.eListView.setOnGroupExpandListener(new GroupExpandListener());
- this.eListView.setOnGroupCollapseListener(new GroupCollapseListener());
-
- }
-
- // /1,子選項點擊事件
- private class ChildClickListener implements OnChildClickListener {
-
- public boolean onChildClick(ExpandableListView parent, View v,
- int groupPosition, int childPosition, long id) {
- Toast
- .makeText(
- MaintActivity.this,
- "子選項被選中,所的組:" + groupPosition + " 子選項的位置:"
- + childPosition, Toast.LENGTH_LONG).show();
- return false;
- }
-
- }
-
- // 2。父選項單擊事件
- private class GroupClickListener implements OnGroupClickListener {
-
- public boolean onGroupClick(ExpandableListView parent, View v,
- int groupPosition, long id) {
- Toast.makeText(MaintActivity.this, "分組選項被選中,所在組: " + groupPosition,
- Toast.LENGTH_LONG).show();
- return false;
- }
-
- }
-
- // 3.分組打開事件
- private class GroupExpandListener implements OnGroupExpandListener {
-
- public void onGroupExpand(int groupPosition) {
- // TODO Auto-generated method stub
- Toast.makeText(MaintActivity.this, "打開分組,所在組:" + groupPosition,
- Toast.LENGTH_LONG).show();
- }
-
- }
-
- // 4.分組關閉事件
- private class GroupCollapseListener implements OnGroupCollapseListener {
-
- public void onGroupCollapse(int groupPosition) {
- Toast.makeText(MaintActivity.this, "關閉分組,所在組:" + groupPosition,
- Toast.LENGTH_LONG).show();
- }
-
- }
-
- }
3.主活動的實現需要一個適配器,myExpendableadapler.java
- package com.wang;
-
- import android.content.Context;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.TextView;
-
- public class myExpendableadapler extends BaseExpandableListAdapter {
- // 組名稱
- private String[] group = new String[] { "專業課", "公共課", "選修課", "選逃課" };
- // 子選項的名字
- private String[][] child = new String[][] {
- { "C語言", "java程序設計基礎教程", "數據庫原理與應用", "數據結構", "linux下的嵌入式C語言編程",
- "linux桌面應用程序設計", "計算機操作系統", "計算機組成原理" },
- { "大學英語", "馬克思哲學原理", "形勢與政策", "ARM體系結構", "嵌入式軟件開發", "軟件工程" },
- { "音樂鑒賞", "市場營銷", "android開發與應用", "Visual C++實用教程", "算法分析與程序設計" },
- { "馬克思哲學原理", "形勢與政策" } };
- private Context context = null;
-
- // 構造函數
- public myExpendableadapler(Context context) {
-
- this.context = context;
- }
-
- public Object getChild(int groupPosition, int childPosition) {
-
- return this.child[groupPosition][childPosition];
- }
-
- public long getChildId(int groupPosition, int childPosition) {
-
- return childPosition;
- }
-
- private TextView buildTextView() {
- //LayoutParams AbsListView擴展提供一個位置來保存視圖類型。
- AbsListView.LayoutParams params = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT, 40);
-
- TextView textView = new TextView(this.context);
- textView.setLayoutParams(params);
- //大小
- textView.setTextSize(15.0f);
- textView.setGravity(Gravity.LEFT+3);
- textView.setPadding(40, 8, 3, 3);
- return textView;
- }
-
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- TextView textView = new TextView(this.context);
- //得到每組的子選項並轉換成字符串
- textView.setText(this.getChild(groupPosition, childPosition).toString());
-
- return textView;
- }
- //統計子選項的個數
- public int getChildrenCount(int groupPosition) {
- // TODO Auto-generated method stub
- return this.child[groupPosition].length;
- }
- //得到復選項的位置
- public Object getGroup(int groupPosition) {
- // TODO Auto-generated method stub
- return this.group[groupPosition];
- }
- //得到復選項的個數
- public int getGroupCount() {
- // TODO Auto-generated method stub
- return this.group.length;
- }
- //得到復選項的id
- public long getGroupId(int groupPosition) {
- // TODO Auto-generated method stub
- return groupPosition;
- }
-
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- TextView textView = this.buildTextView();
- textView.setText(this.getGroup(groupPosition).toString());
- return textView;
- }
- //是否子選項和父選項id是穩定在改變底層數據。
- public boolean hasStableIds() {
- // TODO Auto-generated method stub
- return true;
- }
- //p判斷子選項是否可以選擇
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return true;
- }
-
- }
4.接著就可以看看實現的效果了: