ExpandableListView二级列表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ExpandableListView二级列表相关的知识,希望对你有一定的参考价值。
XMl
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 tools:context=".MainActivity" >
6
7 <ExpandableListView
8 android:id="@+id/elv"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11
12 ></ExpandableListView>
13
14 </RelativeLayout>
MainActivity
1 package com.bawei.d2;
2
3 import android.os.Bundle;
4 import android.app.Activity;
5 import android.view.Menu;
6 import android.view.View;
7 import android.view.ViewGroup;
8 import android.widget.BaseExpandableListAdapter;
9 import android.widget.ExpandableListView;
10 import android.widget.ExpandableListView.OnChildClickListener;
11 import android.widget.TextView;
12 import android.widget.Toast;
13
14 public class MainActivity extends Activity {
15
16 private String[][] secondNames;
17 private String[] names;
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23 ExpandableListView elv=(ExpandableListView) findViewById(R.id.elv);
24
25 names = new String[]{" 第一组"," 第二组"," 第三组"," 第四组"};
26 secondNames = new String[][]{
27 {"a","b","c"},
28 {"d","e","f"},
29 {"q","w","e","r"},
30 {"s","d"}
31 };
32
33 elv.setAdapter(new BaseExpandableListAdapter() {
34
35 @Override
36 //控制二级列表条目是否可以点击
37 public boolean isChildSelectable(int groupPosition, int childPosition) {
38 // TODO Auto-generated method stub
39 return true;
40 }
41
42 @Override
43
44 public boolean hasStableIds() {
45 // TODO Auto-generated method stub
46 return false;
47 }
48
49 @Override
50 //返回一级列表的条目布局
51 public View getGroupView(int groupPosition, boolean isExpanded,
52 View convertView, ViewGroup parent) {
53 View view=View.inflate(MainActivity.this,android.R.layout.simple_list_item_1,null);
54 TextView textView= (TextView) view.findViewById(android.R.id.text1);
55 textView.setText(names[groupPosition]);
56 return view;
57 }
58
59 @Override
60 //获取一级列表给定的Id
61 public long getGroupId(int groupPosition) {
62 // TODO Auto-generated method stub
63 return groupPosition;
64 }
65
66 @Override
67 //第一列表的个数
68 public int getGroupCount() {
69 // TODO Auto-generated method stub
70 return names.length;
71 }
72
73 @Override
74 //获取给定的一级列表的数据
75 public Object getGroup(int groupPosition) {
76 // TODO Auto-generated method stub
77 return groupPosition;
78 }
79
80 @Override
81 //第二级列表的个数
82 public int getChildrenCount(int groupPosition) {
83 // TODO Auto-generated method stub
84 return secondNames[groupPosition].length;
85 }
86
87 @Override
88 //返回二级列表的条目布局
89
90 public View getChildView(int groupPosition, int childPosition,
91 boolean isLastChild, View convertView, ViewGroup parent) {
92 View view= View.inflate(MainActivity.this,android.R.layout.simple_list_item_1, null);
93 TextView textView=(TextView) view.findViewById(android.R.id.text1);
94 textView.setText(secondNames[groupPosition][childPosition]);
95 return view;
96 }
97
98 @Override
99 //获取二级列表给定的ID
100 public long getChildId(int groupPosition, int childPosition) {
101 // TODO Auto-generated method stub
102 return childPosition;
103 }
104
105 @Override
106 //获取给定的二级列表的数据
107 public Object getChild(int groupPosition, int childPosition) {
108 // TODO Auto-generated method stub
109 return childPosition;
110 }
111 }); //二级列表监听
112 elv.setOnChildClickListener(new OnChildClickListener() {
113
114 @Override
115 public boolean onChildClick(ExpandableListView parent, View v,
116 int groupPosition, int childPosition, long id) {
117 Toast.makeText(MainActivity.this, "你点击了第"+(groupPosition+1)+"组的第"+(childPosition+1)+"项", 0).show();
118 return false;
119 }
120 });
121
122
123
124
125 }
126
127
128 }
以上是关于ExpandableListView二级列表的主要内容,如果未能解决你的问题,请参考以下文章
(转载)自定义ExpandableListView,实现二级列表效果