ExpandableListView==>可展开的列表组件
==》
ExpandableListView是ListView的子类,对其进行了扩展,其将应用中的列表项分为几组,每组中又包含多个列表项;
ExpandableListView的用法和ListView非常像,只是其所显示的列表项应该由ExpandableListAdapter提供;
ExpandableListView支持的额外属性:
android:childDivider | 指定各组内各子列表项之间的分隔条 |
android:childIndicator | 显示在子列表项旁的Drawable对象 |
android:groupIndicator | 显示在组列表项旁的Drawable对象 |
实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | 布局文件==》 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <ExpandableListView android:id= "@+id/list" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:background= "#ffffff" android:cacheColorHint= "#00000000" android:listSelector= "#00000000" > </ExpandableListView> </LinearLayout> 实现代码==》 package com.example.myexpandablelistview; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { // 设置组视图的图片 private int [] logos = new int [] { R.drawable.one, R.drawable.two, R.drawable.three }; // 设置组视图的显示文字 private String[] generalsTypes = new String[] { "One" , "Two" , "Three" }; // 子视图显示文字 private String[][] generals = new String[][] { { "西瓜" , "樱桃" , "草莓" }, { "葡萄" , "梨子" , "青苹果" }, { "香蕉" , "橙子" , "芒果" } }; // 子视图图片 public int [][] generallogos = new int [][] { { R.drawable.one, R.drawable.one, R.drawable.one }, { R.drawable.two, R.drawable.two, R.drawable.two, }, { R.drawable.three, R.drawable.three, R.drawable.three } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); final ExpandableListAdapter adapter = new BaseExpandableListAdapter() { // 自己定义一个获得文字信息的方法 TextView getTextView() { @SuppressWarnings( "deprecation" ) AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView textView = new TextView(MainActivity. this ); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); textView.setTextColor(Color.BLACK); return textView; } // 重写ExpandableListAdapter中的各个方法 @Override public int getGroupCount() { return generalsTypes.length; } @Override public Object getGroup( int groupPosition) { return generalsTypes[groupPosition]; } @Override public long getGroupId( int groupPosition) { return groupPosition; } @Override public int getChildrenCount( int groupPosition) { return generals[groupPosition].length; } @Override public Object getChild( int groupPosition, int childPosition) { return generals[groupPosition][childPosition]; } @Override public long getChildId( int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true ; } @Override public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity. this ); ll.setOrientation(0); ImageView logo = new ImageView(MainActivity. this ); logo.setImageResource(logos[groupPosition]); logo.setPadding(50, 0, 0, 0); ll.addView(logo); TextView textView = getTextView(); textView.setTextColor(Color.BLACK); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity. this ); ll.setOrientation(0); ImageView generallogo = new ImageView(MainActivity. this ); generallogo.setImageResource(generallogos[groupPosition][childPosition]); ll.addView(generallogo); TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); ll.addView(textView); return ll; } @Override public boolean isChildSelectable( int groupPosition, int childPosition) { return true ; } }; ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list); expandableListView.setAdapter(adapter); // 设置item点击的监听器 expandableListView.setOnChildClickListener( new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity. this , "你点击了" + adapter.getChild(groupPosition, childPosition), Toast.LENGTH_SHORT) .show(); return false ; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true ; } } |
实现效果:
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本