喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,今天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,但界面优化方面做得还不够好,有待改进,素材采用了Q版三国杀武将的图片,很有爱哈哈,下面直接上效果图以及源代码~!
main.xml的布局很简单啦,只是一个ExpandableListView 就OK了
但值得简单说下的是android:cacheColorHint="#00000000",这个设置可以去除拖动view时背景变成黑色的效果
android:listSelector="#00000000" ,可以去除选中时的黄色底色
1<?xmlversion="1.0"encoding="utf-8"?>
2<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
3android:layout_width="fill_parent"
4android:layout_height="fill_parent"
5android:orientation="vertical">
6<ExpandableListView
7android:id="@+id/list"
8android:layout_width="fill_parent"
9android:layout_height="fill_parent"
10android:background="#ffffff"
11android:cacheColorHint="#00000000"
12android:listSelector="#00000000"
13>
14 </ExpandableListView>
15</LinearLayout>16
java代码:
packagecom.eyu.activity_test;importandroid.app.Activity;importandroid.graphics.Color;importandroid.os.Bundle;importandroid.view.Gravity;importandroid.view.View;importandroid.view.ViewGroup;importandroid.view.Window;importandroid.widget.AbsListView;importandroid.widget.BaseExpandableListAdapter;importandroid.widget.ExpandableListAdapter;importandroid.widget.ExpandableListView;importandroid.widget.ExpandableListView.OnChildClickListener;importandroid.widget.ImageView;importandroid.widget.LinearLayout;importandroid.widget.TextView;importandroid.widget.Toast;publicclassExpandableListextendsActivity{protectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);finalExpandableListAdapteradapter=newBaseExpandableListAdapter(){//设置组视图的图片
int[]logos=newint[]{R.drawable.wei,R.drawable.shu,R.drawable.wu};//设置组视图的显示文字
privateString[]generalsTypes=newString[]{"魏","蜀","吴"};//子视图显示文字
privateString[][]generals=newString[][]{{"夏侯惇","甄姬","许褚","郭嘉","司马懿","杨修"},{"马超","张飞","刘备","诸葛亮","黄月英","赵云"},{"吕蒙","陆逊","孙权","周瑜","孙尚香"}};//子视图图片
publicint[][]generallogos=newint[][]{{R.drawable.xiahoudun,R.drawable.zhenji,R.drawable.xuchu,R.drawable.guojia,R.drawable.simayi,R.drawable.yangxiu},{R.drawable.machao,R.drawable.zhangfei,R.drawable.liubei,R.drawable.zhugeliang,R.drawable.huangyueying,R.drawable.zhaoyun},{R.drawable.lvmeng,R.drawable.luxun,R.drawable.sunquan,R.drawable.zhouyu,R.drawable.sunshangxiang}};//自己定义一个获得文字信息的方法
TextViewgetTextView(){AbsListView.LayoutParamslp=newAbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,64);TextViewtextView=newTextView(ExpandableList.this);textView.setLayoutParams(lp);textView.setGravity(Gravity.CENTER_VERTICAL);textView.setPadding(36,0,0,0);textView.setTextSize(20);textView.setTextColor(Color.BLACK);returntextView;}//重写ExpandableListAdapter中的各个方法
@OverridepublicintgetGroupCount(){//TODOAuto-generatedmethodstub
returngeneralsTypes.length;}@OverridepublicObjectgetGroup(intgroupPosition){//TODOAuto-generatedmethodstub
returngeneralsTypes[groupPosition];}@OverridepubliclonggetGroupId(intgroupPosition){//TODOAuto-generatedmethodstub
returngroupPosition;}@OverridepublicintgetChildrenCount(intgroupPosition){//TODOAuto-generatedmethodstub
returngenerals[groupPosition].length;}@OverridepublicObjectgetChild(intgroupPosition,intchildPosition){//TODOAuto-generatedmethodstub
returngenerals[groupPosition][childPosition];}@OverridepubliclonggetChildId(intgroupPosition,intchildPosition){//TODOAuto-generatedmethodstub
returnchildPosition;}@OverridepublicbooleanhasStableIds(){//TODOAuto-generatedmethodstub
returntrue;}@OverridepublicViewgetGroupView(intgroupPosition,booleanisExpanded,ViewconvertView,ViewGroupparent){//TODOAuto-generatedmethodstub
LinearLayoutll=newLinearLayout(ExpandableList.this);ll.setOrientation(0);ImageViewlogo=newImageView(ExpandableList.this);logo.setImageResource(logos[groupPosition]);logo.setPadding(50,0,0,0);ll.addView(logo);TextViewtextView=getTextView();textView.setTextColor(Color.BLACK);textView.setText(getGroup(groupPosition).toString());ll.addView(textView);returnll;}@OverridepublicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild,ViewconvertView,ViewGroupparent){//TODOAuto-generatedmethodstub
LinearLayoutll=newLinearLayout(ExpandableList.this);ll.setOrientation(0);ImageViewgenerallogo=newImageView(ExpandableList.this);generallogo.setImageResource(generallogos[groupPosition][childPosition]);ll.addView(generallogo);TextViewtextView=getTextView();textView.setText(getChild(groupPosition,childPosition).toString());ll.addView(textView);returnll;}@OverridepublicbooleanisChildSelectable(intgroupPosition,intchildPosition){//TODOAuto-generatedmethodstub
returntrue;}};ExpandableListViewexpandableListView=(ExpandableListView)findViewById(R.id.list);expandableListView.setAdapter(adapter);//设置item点击的监听器
expandableListView.setOnChildClickListener(newOnChildClickListener(){@OverridepublicbooleanonChildClick(ExpandableListViewparent,Viewv,intgroupPosition,intchildPosition,longid){Toast.makeText(ExpandableList.this,"你点击了"+adapter.getChild(groupPosition,childPosition),Toast.LENGTH_SHORT).show();returnfalse;}});}}
分享到:
相关推荐
一个ExpandableListView的例子一个ExpandableListView的例子
一个ExpandableListView的例子,实现多级菜单分类展示.rar
你也可能已经看到ExpandableListView一个标准的实现。然而,最近我的项目需要在屏幕方面使用这两种类型的列表。新的屏幕是有一个可折叠列表(1组ExpandableListview)的和它下面的一个标准的ListView。复杂的问题,...
关于ExpandableListView的控件,有个需求就是之展开一条,写了一个例子上传到这里,和大家交流学习
expandablelistview的例子。。实现添加信息更新listview
最近在做关于伸缩列表这块功能,在网上找了许多关于ExpandableListView+CheckBox的例子,不是代码比较臃肿,就是写法混乱,都没什么参考意义,所以动手写了个简洁易懂的Demo.
xamarin android expandableListView一个二级的listview学习实例,使用比较简单和listview其实差不多,二级的listview也是比较常用的控件。
安卓开发-一个ExpandableListView的例子,实现多级菜单分类展示
android ExpandableListView简单例子
一个ExpandableListView的例子,实现多级菜单分类展示.zip安卓程序源码资源下载一个ExpandableListView的例子,实现多级菜单分类展示.zip安卓程序源码资源下载 1.适合学生做毕业设计用 2.适合程序员学习研究用 3....
安卓Andriod源码——一个ExpandableListView的例子,实现多级菜单分类展示.zip
安卓Android源码——一个ExpandableListView的例子,实现多级菜单分类展示.zip
一个ExpandableListView的例子,实现多级菜单分类展示.zip项目安卓应用源码下载一个ExpandableListView的例子,实现多级菜单分类展示.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3...
Android高级应用源码-一个ExpandableListView的例子,实现多级菜单分类展示.zip
源码参考,欢迎下载
一个ExpandableListView的例子,实现多级菜单分类展示.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
用了两个item实现 ExpandableListView。