您的位置:首页 > 其它

ExpandableListView组件分组用法

2016-03-08 22:56 393 查看
                                                         ExpandableListView组件分组用法              

     ExpandableListView组件是android中一个比较常用的组件,当点击一个父item的时候可以将它的子item显示出来,像QQ中的好友列表就是实现的类型效果。使用ExpandableListView组件的关键就是设置它的adapter,这个adapter必须继承BaseExpandbaleListAdapter类,所以实现运用ExpandableListView的核心就是学会继承这个BaseExpanableListAdapter类

首先建立3个xml文件



1.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ExpandableListView
android:id="@+id/myExpandListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</RelativeLayout>


2.layout_children.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_children"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


3.layout_parent.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


最后就是java代码了MainActivity.java,详情见注释

public class MainActivity extends Activity {
private ExpandableListView expandableListView;
private List<String> parentList =new ArrayList<String>();
private Map<String,List<String>> map =new HashMap<String,List<String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.myExpandListView);
initData();
expandableListView.setAdapter(new MyAdapter());
}

private void initData() {
parentList.clear();
map.clear();
parentList.add("friend");
parentList.add("high");
parentList.add("classM");
List<String> list1 =new ArrayList<String>();
{
list1.clear();
list1.add("曹老师");
list1.add("高敏");
list1.add("宋慧乔");
map.put("friend",list1);
}
List<String> list2 =new ArrayList<String>();
{
list2.clear();
list2.add("袁妙");
list2.add("茅岗");
list2.add("阿全");
map.put("high",list2);
}
List<String> list3 =new ArrayList<String>();
{
list3.clear();
list3.add("老潘");
list3.add("郑大神");
list3.add("娄大神");
map.put("classM",list3);
}
}
class MyAdapter extends BaseExpandableListAdapter{
@Override
public int getGroupCount() {
return parentList.size();
}
//获取当前父item下的子item的歌手
@Override
public int getChildrenCount(int groupPosition) {
String key =parentList.get(groupPosition);
return map.get(key).size();
}

@Override
public Object getGroup(int groupPosition) {
return parentList.get(groupPosition);
}
//获取子item对象
@Override
public Object getChild(int groupPosition, int childPosition) {
String key=parentList.get(groupPosition);
return map.get(key).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@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) {
if(convertView==null){
LayoutInflater inflater= (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView =inflater.inflate(R.layout.layout_parent,null);
}
TextView tv= (TextView) convertView.findViewById(R.id.tv_parent);
tv.setText(MainActivity.this.parentList.get(groupPosition));
return tv;
}
//子组件和数据绑定
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String key = parentList.get(groupPosition);
String text =map.get(key).get(childPosition);
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView =inflater.inflate(R.layout.layout_children,null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tv_children);
tv.setText(text);
return tv;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}


运行结果,样式设置太简单,有点丑,可以使用官方提供的android.R.layout.simple_expandable_list_item_1

和android.R.layout.simple_expandable_list+item_2,个人建议去参照我的第一篇博文,加一张图片和两行文本

推荐使用相对布局.你要相信只要你想写就能成功.



 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  控件 实例 图片