歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中的多級列表的應用

Android中的多級列表的應用

日期:2017/3/1 10:14:07   编辑:Linux編程
看看今天實現的Android中個多級列表的功能,其實這就是一個小組件,但是如果用好了,可以實現很大的功能呢!接著,有點累,什麼都不說了,看看看實現的過程就可以了,挺簡單的!

1.看看布局文件main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="@drawable/bg"
  7. >
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="那些上過與即將被上的大學課程:"
  12. android:textColor="#FF6100"
  13. android:textSize="20dp"
  14. />
  15. <ExpandableListView
  16. android:id="@+id/elistview"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_marginLeft="5dp"
  20. />
  21. </LinearLayout>
2.接著看看主活動mainActivity.java
  1. package com.wang;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.ContextMenu;
  5. import android.view.View;
  6. import android.view.Window;
  7. import android.view.WindowManager;
  8. import android.view.ContextMenu.ContextMenuInfo;
  9. import android.widget.ExpandableListAdapter;
  10. import android.widget.ExpandableListView;
  11. import android.widget.Toast;
  12. import android.widget.ExpandableListView.OnChildClickListener;
  13. import android.widget.ExpandableListView.OnGroupClickListener;
  14. import android.widget.ExpandableListView.OnGroupCollapseListener;
  15. import android.widget.ExpandableListView.OnGroupExpandListener;
  16. public class MaintActivity extends Activity {
  17. // 創建一個上下文菜單的方法
  18. public void onCreateContextMenu(ContextMenu menu, View v,
  19. ContextMenuInfo menuInfo) {
  20. super.onCreateContextMenu(menu, v, menuInfo);
  21. // 一個垂直滾動的兩級列表。取得菜單項
  22. ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
  23. // 獲得這個類型 的位置
  24. int type = ExpandableListView
  25. .getPackedPositionType(info.packedPosition);
  26. // 取得所在組的索引
  27. int group = ExpandableListView
  28. .getPackedPositionGroup(info.packedPosition);
  29. // 取得子菜單的索引
  30. int child = ExpandableListView
  31. .getPackedPositionGroup(info.packedPosition);
  32. Toast.makeText(MaintActivity.this,
  33. "類型 =" + type + " 分組:" + group + " 子選項:" + child,
  34. Toast.LENGTH_LONG).show();
  35. }
  36. private ExpandableListView eListView = null;
  37. private ExpandableListAdapter adapter = null;
  38. @Override
  39. public void onCreate(Bundle savedInstanceState) {
  40. // 去除標題,
  41. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  42. // 取消狀態欄,充滿全屏
  43. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  44. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  45. super.onCreate(savedInstanceState);
  46. super.setContentView(R.layout.main);
  47. // 實例化組件
  48. this.eListView = (ExpandableListView) findViewById(R.id.elistview);
  49. // 聲明一個adapter對象
  50. adapter = new myExpendableadapler(this);
  51. // 設置適配器提供了數據
  52. this.eListView.setAdapter(this.adapter);
  53. //  注冊一個上下文菜單顯示給定的視圖(多個視圖可以顯示上下文菜單)。
  54. super.registerForContextMenu(this.eListView);
  55. // 設置點擊時候觸發的事件 1,子選項點擊事件 2。父選項單擊事件 3.分組打開事件 4.分組關閉事件
  56. this.eListView.setOnChildClickListener(new ChildClickListener());
  57. this.eListView.setOnGroupClickListener(new GroupClickListener());
  58. this.eListView.setOnGroupExpandListener(new GroupExpandListener());
  59. this.eListView.setOnGroupCollapseListener(new GroupCollapseListener());
  60. }
  61. // /1,子選項點擊事件
  62. private class ChildClickListener implements OnChildClickListener {
  63. public boolean onChildClick(ExpandableListView parent, View v,
  64. int groupPosition, int childPosition, long id) {
  65. Toast
  66. .makeText(
  67. MaintActivity.this,
  68. "子選項被選中,所的組:" + groupPosition + " 子選項的位置:"
  69. + childPosition, Toast.LENGTH_LONG).show();
  70. return false;
  71. }
  72. }
  73. // 2。父選項單擊事件
  74. private class GroupClickListener implements OnGroupClickListener {
  75. public boolean onGroupClick(ExpandableListView parent, View v,
  76. int groupPosition, long id) {
  77. Toast.makeText(MaintActivity.this, "分組選項被選中,所在組: " + groupPosition,
  78. Toast.LENGTH_LONG).show();
  79. return false;
  80. }
  81. }
  82. // 3.分組打開事件
  83. private class GroupExpandListener implements OnGroupExpandListener {
  84. public void onGroupExpand(int groupPosition) {
  85. // TODO Auto-generated method stub
  86. Toast.makeText(MaintActivity.this, "打開分組,所在組:" + groupPosition,
  87. Toast.LENGTH_LONG).show();
  88. }
  89. }
  90. // 4.分組關閉事件
  91. private class GroupCollapseListener implements OnGroupCollapseListener {
  92. public void onGroupCollapse(int groupPosition) {
  93. Toast.makeText(MaintActivity.this, "關閉分組,所在組:" + groupPosition,
  94. Toast.LENGTH_LONG).show();
  95. }
  96. }
  97. }
3.主活動的實現需要一個適配器,myExpendableadapler.java
  1. package com.wang;
  2. import android.content.Context;
  3. import android.view.Gravity;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.AbsListView;
  7. import android.widget.BaseExpandableListAdapter;
  8. import android.widget.TextView;
  9. public class myExpendableadapler extends BaseExpandableListAdapter {
  10. // 組名稱
  11. private String[] group = new String[] { "專業課", "公共課", "選修課", "選逃課" };
  12. // 子選項的名字
  13. private String[][] child = new String[][] {
  14. { "C語言", "java程序設計基礎教程", "數據庫原理與應用", "數據結構", "linux下的嵌入式C語言編程",
  15. "linux桌面應用程序設計", "計算機操作系統", "計算機組成原理" },
  16. { "大學英語", "馬克思哲學原理", "形勢與政策", "ARM體系結構", "嵌入式軟件開發", "軟件工程" },
  17. { "音樂鑒賞", "市場營銷", "android開發與應用", "Visual C++實用教程", "算法分析與程序設計" },
  18. { "馬克思哲學原理", "形勢與政策" } };
  19. private Context context = null;
  20. // 構造函數
  21. public myExpendableadapler(Context context) {
  22. this.context = context;
  23. }
  24. public Object getChild(int groupPosition, int childPosition) {
  25. return this.child[groupPosition][childPosition];
  26. }
  27. public long getChildId(int groupPosition, int childPosition) {
  28. return childPosition;
  29. }
  30. private TextView buildTextView() {
  31. //LayoutParams AbsListView擴展提供一個位置來保存視圖類型。
  32. AbsListView.LayoutParams params = new AbsListView.LayoutParams(
  33. ViewGroup.LayoutParams.FILL_PARENT, 40);
  34. TextView textView = new TextView(this.context);
  35. textView.setLayoutParams(params);
  36. //大小
  37. textView.setTextSize(15.0f);
  38. textView.setGravity(Gravity.LEFT+3);
  39. textView.setPadding(40, 8, 3, 3);
  40. return textView;
  41. }
  42. public View getChildView(int groupPosition, int childPosition,
  43. boolean isLastChild, View convertView, ViewGroup parent) {
  44. TextView textView = new TextView(this.context);
  45. //得到每組的子選項並轉換成字符串
  46. textView.setText(this.getChild(groupPosition, childPosition).toString());
  47. return textView;
  48. }
  49. //統計子選項的個數
  50. public int getChildrenCount(int groupPosition) {
  51. // TODO Auto-generated method stub
  52. return this.child[groupPosition].length;
  53. }
  54. //得到復選項的位置
  55. public Object getGroup(int groupPosition) {
  56. // TODO Auto-generated method stub
  57. return this.group[groupPosition];
  58. }
  59. //得到復選項的個數
  60. public int getGroupCount() {
  61. // TODO Auto-generated method stub
  62. return this.group.length;
  63. }
  64. //得到復選項的id
  65. public long getGroupId(int groupPosition) {
  66. // TODO Auto-generated method stub
  67. return groupPosition;
  68. }
  69. public View getGroupView(int groupPosition, boolean isExpanded,
  70. View convertView, ViewGroup parent) {
  71. // TODO Auto-generated method stub
  72. TextView textView = this.buildTextView();
  73. textView.setText(this.getGroup(groupPosition).toString());
  74. return textView;
  75. }
  76. //是否子選項和父選項id是穩定在改變底層數據。
  77. public boolean hasStableIds() {
  78. // TODO Auto-generated method stub
  79. return true;
  80. }
  81. //p判斷子選項是否可以選擇
  82. public boolean isChildSelectable(int groupPosition, int childPosition) {
  83. // TODO Auto-generated method stub
  84. return true;
  85. }
  86. }
4.接著就可以看看實現的效果了:

Copyright © Linux教程網 All Rights Reserved