一种用于垂直滚动展示两级列表的视图,和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选项。这些选项的数据是通过 ExpandableListAdapter 关联的。packagecom.example.expandablelistr;importandroid.app.Activity;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseExpandableListAdapter;importandroid.widget.ExpandableListView;importandroid.widget.ExpandableListView.OnChildClickListener;importandroid.widget.Toast;importandroid.widget.ExpandableListView.OnGroupClickListener;importandroid.widget.TextView;publicclassMainActivityextendsActivity{ ExpandableListView expandableListView; String[] groupStrings = {"西游记","水浒传","三国演义","红楼梦"}; String[][] childStrings = { {"唐三藏","孙悟空","猪八戒","沙和尚"}, {"宋江","林冲","李逵","鲁智深"}, {"曹操","刘备","孙权","诸葛亮","周瑜"}, {"贾宝玉","林黛玉","薛宝钗","王熙凤"}};@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); expandableListView=(ExpandableListView) findViewById(R.id.expandableListView); myadapter myadapter =newmyadapter(); expandableListView.setAdapter(myadapter);//点击组的监听expandableListView.setOnGroupClickListener(newOnGroupClickListener() {@OverridepublicbooleanonGroupClick(ExpandableListView parent, View v,intgroupPosition,longid){// TODO Auto-generated method stubToast.makeText(getApplication(), groupStrings[groupPosition],0).show();;returnfalse;//返回false不然分组不会展开} }); expandableListView.setOnChildClickListener(newOnChildClickListener() {@OverridepublicbooleanonChildClick(ExpandableListView parent, View v,intgroupPosition,intchildPosition,longid){// TODO Auto-generated method stubToast.makeText(getApplication(), childStrings[groupPosition][childPosition],0).show();;returnfalse; } }); }classmyadapterextendsBaseExpandableListAdapter{//分组的个数@OverridepublicintgetGroupCount(){// TODO Auto-generated method stubreturngroupStrings.length; }//获取指定分组中的子选项的个数@OverridepublicintgetChildrenCount(intgroupPosition){// TODO Auto-generated method stubreturnchildStrings[groupPosition].length; }//获取指定的分组数据@OverridepublicObjectgetGroup(intgroupPosition){// TODO Auto-generated method stubreturngroupStrings[groupPosition]; }//获取指定分组中指定的子选项的数据@OverridepublicObjectgetChild(intgroupPosition,intchildPosition){// TODO Auto-generated method stubreturnchildStrings[groupPosition][childPosition]; }//获取指定分组的ID@OverridepubliclonggetGroupId(intgroupPosition){// TODO Auto-generated method stubreturngroupPosition; }//获取指定分组下的子选项ID@OverridepubliclonggetChildId(intgroupPosition,intchildPosition){// TODO Auto-generated method stubreturnchildPosition; }//分组和子选项是否持有稳定的ID,也就是底层数据的改变会不会影响到他们@OverridepublicbooleanhasStableIds(){// TODO Auto-generated method stubreturntrue; }//获取指定分组的视图(isExpanded是否扩展)@OverridepublicViewgetGroupView(intgroupPosition,booleanisExpanded, View convertView, ViewGroup parent){// TODO Auto-generated method stubGroupViewHolder groupViewHolder=null;if(convertView==null){ convertView=View.inflate(MainActivity.this,R.layout.hzu,null); groupViewHolder=newGroupViewHolder(); groupViewHolder.tvTitle=(TextView) convertView.findViewById(R.id.label_expand_group); convertView.setTag(groupViewHolder); }else{ groupViewHolder = (GroupViewHolder) convertView.getTag(); } groupViewHolder.tvTitle.setText(groupStrings[groupPosition]);returnconvertView; }//获取指定分组中的指定子选项的视图@OverridepublicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild, View convertView, ViewGroup parent){// TODO Auto-generated method stubChildViewHolder childViewHolder=null;if(convertView==null){ childViewHolder=newChildViewHolder(); convertView=View.inflate(MainActivity.this,R.layout.hzu2,null); childViewHolder.tvTitle=(TextView)convertView.findViewById(R.id.label_expand_group2); convertView.setTag(childViewHolder);//else这句话有用}else{ childViewHolder = (ChildViewHolder) convertView.getTag(); } Log.e("set","scoss"); childViewHolder.tvTitle.setText(childStrings[groupPosition][childPosition]);returnconvertView; }//指定位置上的子元素是否可选中@OverridepublicbooleanisChildSelectable(intgroupPosition,intchildPosition){// TODO Auto-generated method stubreturntrue; } }classGroupViewHolder{ TextView tvTitle; }classChildViewHolder{ TextView tvTitle;}}android:id="@+id/expandableListView"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/label_expand_group"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:background="@android:color/holo_blue_light"android:paddingLeft="20dp"android:textColor="@android:color/white"android:textSize="30sp"android:id="@+id/label_expand_group2"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:textColor="#fff"android:background="@android:color/darker_gray"android:textSize="20sp"
ExpandableListView
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 關於ExpandableListView心得DEMO放在這給自己留個紀錄 1.設定group.xml 2.設定ch...
- 先说一下这篇文章里面的内容:TCP 客户端, 自定义对话框, 自定义按钮, ProgressBar竖直显示, 重力...
- 转载自【移动开发】Android中三种超实用的滑屏方式汇总(ViewPager、ViewFlipper、ViewF...
- 前言 最近公司项目里面有一个页面需要用到很多的设置功能,绞尽脑汁的我最终想到使用折叠卡片的形式来布局我的界面,效果...