700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android 仿QQ好友分组列表 ExpandableListView的使用详解

Android 仿QQ好友分组列表 ExpandableListView的使用详解

时间:2021-06-14 16:25:13

相关推荐

Android 仿QQ好友分组列表 ExpandableListView的使用详解

ListView只能显示一级列表,如果我们需要像QQ好友列表的那样的效果,就需要用到ExpandableListView,入门新手可能对该控件不是很熟悉,下面就详解一下基本用法,其实跟ListView差不多,下面来说一下具体的使用方法把!

效果图:

首先,布局中加入

<ExpandableListViewandroid:id ="@+id/expandableListView"android:layout_width ="fill_parent"android:layout_height ="wrap_content"android:groupIndicator="@null" />

然后,在activity中设置adapter,这里需要注意的是adapter的使用,我们这里的adapter继承的是BaseExpandableListAdapter

先初始一下数据,

public String[] groups = { "魏", "蜀", "吴" };public String[][] children = {{ "曹操", "荀彧", "郭嘉", "夏侯惇", "许褚"},{ "刘备", "诸葛亮", "关羽", "赵云", "庞统", "魏延", "马超" },{ "孙权", "周瑜", "鲁肃", "黄盖", "吕蒙"},};

然后设置adapter和点击监听

ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);expandableListView.setAdapter(new ExpandableAdapter(groups,children));//设置子条目的点击监听expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {Toast.makeText(MainActivity.this, "当前点击的是::"+groups[groupPosition]+"国的"+children[groupPosition][childPosition], Toast.LENGTH_SHORT).show();//这里return true的话子列表不会展开 return false才展开return false;}});

写一个自定义adapter继承BaseExpandableListAdapter

public class ExpandableAdapter extends BaseExpandableListAdapter{public String[] groups;public String[][] children;public ExpandableAdapter(String[] groups, String[][] children) {this.groups = groups;this.children = children;}//获取与给定的组相关的数据,得到数组groups中元素的数据public Object getGroup(int groupPosition) {return groups[groupPosition];}//获取与孩子在给定的组相关的数据,得到数组children中元素的数据public Object getChild(int groupPosition, int childPosition) {return children[groupPosition][childPosition];}//获取的群体数量,得到groups里元素的个数public int getGroupCount() {return groups.length;}//取得指定组中的children个数,就是groups中每一个条目中的个数public int getChildrenCount(int groupPosition) {return children[groupPosition].length;}//获取组在给定的位置编号,即groups中元素的IDpublic long getGroupId(int groupPosition) {return groupPosition;}//获取在给定的组的children的ID,也就是children中元素的IDpublic long getChildId(int groupPosition, int childPosition) {return childPosition;}//获取一个视图显示给定组,存放groupspublic View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) {TextView textView = getGenericView(24);textView.setText(getGroup(groupPosition).toString());return textView;}//获取一个视图显示在给定的组 的儿童的数据,就是存放childrenpublic View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {TextView textView = getGenericView(18);textView.setText(getChild(groupPosition, childPosition).toString());return textView;}//孩子在指定的位置是可选的,即:children中的元素是可点击的public boolean isChildSelectable(int groupPosition, int childPosition) {return true;}//表示孩子是否和组ID是跨基础数据的更改稳定public boolean hasStableIds() {return true;}//自定义的创建TextViewpublic TextView getGenericView(int mTextSize) {// Layout parameters for the ExpandableListViewAbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);TextView textView = new TextView(MainActivity.this);textView.setLayoutParams(lp);textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);textView.setPadding(42, 12, 12, 12);textView.setTextSize(mTextSize);textView.setTextColor(Color.BLACK);return textView;}}

OK!完成,运行一下看效果吧!想要美观一些需要在adapter的getChildView和getGroupView中加载自定义的布局文件,类似于BaseAdapter中的getView里的一样,如果条目过多的话注意别忘了在getChildView和getGroupView中使用ViewHolder!

好了,本次教程就这些,有什么错误的地方欢迎指点

本文相关下载:点击免费下载源码及Apk文件

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。