2012-11-25 16:30:04.0|分类: android|浏览量: 1622
项目中需要类似qq好友的通讯录,所以就写了一个简单的例子,分享一下。 效果:
1、MainActivity.java的代码:
package com.tbx.android; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ExpandableListView; public class MainActivity extends Activity { ExpendAdapter adapter; ExpandableListView exList; private List<GroupElement> parentList = new ArrayList<GroupElement>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); exList = (ExpandableListView) this.findViewById(R.id.expandableListView1); initData(); adapter = new ExpendAdapter(MainActivity.this, parentList); exList.setAdapter(adapter); exList.setGroupIndicator(null); exList.setDivider(null); } private void initData() { GroupElement group1 = new GroupElement(); ChildElement childElement = new ChildElement(); childElement.setMood("我喜欢王"); childElement.setResoureid(R.drawable.f009); childElement.setTitle("小鸟"); group1.putChildElement(childElement); childElement = new ChildElement(); childElement.setMood("我喜欢王"); childElement.setResoureid(R.drawable.f010); childElement.setTitle("android"); group1.putChildElement(childElement); group1.setTitle("我的好友"); parentList.add(group1); GroupElement group2 = new GroupElement(); childElement = new ChildElement(); childElement.setMood("我喜欢王"); childElement.setResoureid(R.drawable.f009); childElement.setTitle("小鸟"); group2.putChildElement(childElement); childElement = new ChildElement(); childElement.setMood("我喜欢王"); childElement.setResoureid(R.drawable.f010); childElement.setTitle("android"); group2.putChildElement(childElement); group2.setTitle("高中同学"); parentList.add(group2); } }2、ExpendAdapter.java 代码:
package com.tbx.android; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.*; public class ExpendAdapter extends BaseExpandableListAdapter { private LayoutInflater layoutInflater; private Context mContext; private List<GroupElement> parentList = new ArrayList<GroupElement>(); public ExpendAdapter(Context mContext,List<GroupElement> parentList){ this.mContext = mContext; this.parentList = parentList; layoutInflater = LayoutInflater.from(mContext); } public Object getChild(int groupPosition, int childPosition) { GroupElement group = parentList.get(groupPosition); if(group!=null){ return group.getChildElement(childPosition); } return null; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if(convertView ==null){ convertView = layoutInflater.inflate(R.layout.child, null); } GroupElement groupElement = parentList.get(groupPosition); ChildElement childElement = groupElement.getChildElement(childPosition); if(childElement == null){ return null ; } final ImageView head=(ImageView)convertView.findViewById(R.id.headImage); head.setImageResource(childElement.getResoureid()); final TextView title=(TextView)convertView.findViewById(R.id.title); title.setText(childElement.getTitle()); final TextView mood =(TextView)convertView.findViewById(R.id.mood); mood.setText(childElement.getMood()); return convertView; } @Override public int getChildrenCount(int groupPosition) { GroupElement group = parentList.get(groupPosition); if(group!=null){ return group.getChildSize(); } return 0; } @Override public Object getGroup(int groupPosition) { return parentList.get(groupPosition); } @Override public int getGroupCount() { return parentList.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if(convertView==null){ convertView=layoutInflater.inflate(R.layout.parent, null); } final TextView user_group_name = (TextView) convertView.findViewById(R.id.user_group_name); user_group_name.setText(parentList.get(groupPosition).getTitle()); final TextView user_group_size = (TextView) convertView.findViewById(R.id.user_group_size); int size = parentList.get(groupPosition).getChildSize(); user_group_size.setText("["+size+"]"); final ImageView imageView = (ImageView) convertView.findViewById(R.id.user_group); if(isExpanded){ imageView.setBackgroundResource(R.drawable.group_unfold_arrow); }else{ imageView.setBackgroundResource(R.drawable.group_fold_arrow); } return convertView; } @Override public void onGroupCollapsed(int groupPosition) { super.onGroupCollapsed(groupPosition); } @Override public void onGroupExpanded(int groupPosition) { super.onGroupExpanded(groupPosition); } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { Toast.makeText(mContext, "这是第"+groupPosition+"组,第"+childPosition+"个", Toast.LENGTH_SHORT).show(); return true; } }3、GroupElement.java 代码:
package com.tbx.android; import java.util.ArrayList; public class GroupElement { private String title ; private String img ; private List<ChildElement> childList = new ArrayList<ChildElement>(); public ChildElement getChildElement(int childPosition){ return childList.get(childPosition); } public void putChildElement(ChildElement childElement){ childList.add(childElement); } public int getChildSize(){ return childList.size(); } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImg() { return img; } public void setImg(String img) { this.img = img; } public List<ChildElement> getChildList() { return childList; } public void setChildList(List<ChildElement> childList) { this.childList = childList; } }4、ChildElement.java代码:
public class ChildElement { private String title ; private boolean isOnline ; private int resoureid; //显示图片的资源id private String mood ; //心情 .... }5、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/back1" > <ExpandableListView android:id="@+id/expandableListView1" android:layout_height="wrap_content" android:layout_width="fill_parent"> </ExpandableListView> </LinearLayout>6、parent.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="horizontal" android:id="@+id/parentList" android:layout_height="wrap_content"> <ImageView android:id="@+id/user_group" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/user_group_name" android:textSize="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/user_group_size" android:textSize="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>7、child.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/childList" android:orientation="horizontal" > <ImageView android:paddingLeft="20px" android:id="@+id/headImage" android:src="@drawable/f009" android:layout_width="50px" android:layout_height="50px" android:layout_marginBottom="5px" android:layout_marginRight="10px"/> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/title" android:textSize="18px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/mood" android:textSize="16px" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> 这是所有的主要代码,至于图片我就不上除了,如果那位同学想看看,可以留言。后面会对ExpandableListView详细分析 |