类似于腾讯的QQ好友列表
package com.example.testexpandablelist;
import java.util.List;
import android.app.Activity;
import android.content.ClipData.Item;
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;
//ExpandableListView的Adapter
public class MyExpandableAdapter extends BaseExpandableListAdapter{
Activity activity;
private List<List<MyItem>> mData = null;
private Context mContext;
private LayoutInflater mInflater;
private String[] mGroupStrings;
public MyExpandableAdapter(Context context, List<List<MyItem>> mData2){
mContext = context;
mData = mData2;
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mGroupStrings = mContext.getResources().getStringArray(R.array.groups);
}
@Override
public MyItem getChild(int groupPosition, int childPosition) {
return mData.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean arg2, View convertView,
ViewGroup arg4) {
if(convertView == null){
convertView = mInflater.inflate(R.layout.item_child, null);
}
ChildViewHolder holder = new ChildViewHolder();
holder.mIcon = (ImageView) convertView.findViewById(R.id.p_w_picpathView1_child);
holder.mChildName = (TextView) convertView.findViewById(R.id.textView1_child);
holder.mChildDetail = (TextView) convertView.findViewById(R.id.textView2_child);
holder.mIcon.setBackgroundResource(getChild(groupPosition, childPosition).getImgId());
holder.mChildName.setText(getChild(groupPosition, childPosition).getstrTitle());
holder.mChildDetail.setText(getChild(groupPosition, childPosition).getStrDetail());
return convertView;
}
@Override
public int getChildrenCount(int childPosition) {
return mData.get(childPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return mData.get(groupPosition);
}
@Override
public int getGroupCount() {
return mData.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean arg1, View convertView, ViewGroup arg3) {
if(convertView == null){
convertView = mInflater.inflate(R.layout.item_group, null);
}
GroupViewHolder holder = new GroupViewHolder();
holder.mGroupName = (TextView) convertView.findViewById(R.id.textView1_group);
holder.mGroupCount = (TextView) convertView.findViewById(R.id.textView2_group);
holder.mGroupName.setText(mGroupStrings[groupPosition]);
holder.mGroupCount.setText("[" + mData.get(groupPosition).size() + "]");
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class GroupViewHolder{
TextView mGroupName;
TextView mGroupCount;
}
private class ChildViewHolder{
ImageView mIcon;
TextView mChildName;
TextView mChildDetail;
}
}
//MainActivity
package com.example.testexpandablelist;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
public class MainActivity extends Activity implements OnChildClickListener{
private ExpandableListView mExpandableListView;
private ExpandableListView mListView;
private List<String> groupArray;
private List<List<String>> childArray;
private MyExpandableAdapter mAdapter;
private List<Map<String, Object>> data = null;
private List<Map<String, Object>> list;
private List<List<MyItem>> mData = new ArrayList<List<MyItem>>();
private int[] mGroupArrays = new int[]{
R.array.tianlongbabu,
R.array.shediaoyingxiongzhuan,
R.array.shendiaoxialv
};
private int[] mDetailIds = new int[]{
R.array.tianlongbabu_detail,
R.array.shediaoyingxiongzhuan_detail,
R.array.shendiaoxialv_detail
};
private int[][] mImageIds = new int[][]{
{R.drawable.img_00, R.drawable.img_01, R.drawable.img_02},
{R.drawable.img_10, R.drawable.img_11, R.drawable.img_12, R.drawable.img_13,
R.drawable.img_14, R.drawable.img_15, R.drawable.img_16},
{R.drawable.img_20, R.drawable.img_21}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
mListView = new ExpandableListView(this);
mListView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
setContentView(mListView);
mListView.setGroupIndicator(getResources().getDrawable(
R.drawable.expander_floder));
mAdapter = new MyExpandableAdapter(this, mData);
mListView.setAdapter(mAdapter);
mListView.setDescendantFocusability(ExpandableListView.FOCUS_AFTER_DESCENDANTS);
mListView.setOnChildClickListener(this);
}
/*
* ChildView 设置 布局很可能onChildClick进不来,要在 ChildView layout 里加上
* android:descendantFocusability="blocksDescendants",
* 还有isChildSelectable里返回true
*/
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
MyItem item = mAdapter.getChild(groupPosition, childPosition);
new AlertDialog.Builder(this)
.setTitle(item.getstrTitle())
.setMessage(item.getStrDetail())
.setIcon(mImageIds[groupPosition][childPosition])
.setNegativeButton(R.string.ok, new OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
})
.create()
.show();
return true;
}
private void initData(){
for(int i=0; i<mGroupArrays.length; i++){
List<MyItem> list = new ArrayList<MyItem>();
String[] childs = getStringArray(mGroupArrays[i]);
String[] details = getStringArray(mDetailIds[i]);
for(int j=0; j<childs.length; j++){
MyItem item = new MyItem(mImageIds[i][j], childs[j], details[j]);
list.add(item);
}
mData.add(list);
}
}
private String[] getStringArray(int id){
return getResources().getStringArray(id);
}
}
//MyItem.class
package com.example.testexpandablelist;
public class MyItem {
private int imgId;
private String strTitle;
private String strDetail;
public MyItem(int id, String title, String detail){
imgId = id;
strTitle = title;
strDetail = detail;
}
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
public String getstrTitle() {
return strTitle;
}
public void setstrTitle(String strTitle) {
this.strTitle = strTitle;
}
public String getStrDetail() {
return strDetail;
}
public void setStrDetail(String strDetail) {
this.strDetail = strDetail;
}
public String toString(){
return "Item[" + imgId + ", " + strTitle + ", " + strDetail + "]";
}
}
//arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="groups">
<item>天龙八部</item>
<item>射雕英雄传</item>
<item>神雕侠侣</item>
</string-array>
<string-array name="tianlongbabu">
<item>萧峰</item>
<item>段誉</item>
<item>虚竹</item>
</string-array>
<string-array name="tianlongbabu_detail">
<item>降龙十八掌,打狗棒法,龙爪手</item>
<item>凌波微步,六脉神剑</item>
<item>北冥神功,小无相功,天山折梅手,天山六阳掌</item>
</string-array>
<string-array name="shediaoyingxiongzhuan">
<item>郭靖</item>
<item>黄蓉</item>
<item>王重阳</item>
<item>黄药师</item>
<item>欧阳锋</item>
<item>一灯</item>
<item>洪七公</item>
</string-array>
<string-array name="shediaoyingxiongzhuan_detail">
<item>降龙十八掌,九阴真经,天罡北斗阵,双手互搏,空明拳</item>
<item>碧波掌法,落英神剑掌,玉箫剑法,兰花拂穴手</item>
<item>先天功,金雁功,全真剑法,一剑化三清</item>
<item>***落飞神剑,碧海潮生按玉箫</item>
<item>蛤蟆功,瞬息千里,神驼雪山掌,透骨打穴法</item>
<item>一阳指,先天功</item>
<item>降龙十八掌,打狗棒法,伏虎拳</item>
</string-array>
<string-array name="shendiaoxialv">
<item>杨过</item>
<item>小龙女</item>
</string-array>
<string-array name="shendiaoxialv_detail">
<item>姓杨名过字改之,黯然销魂掌,玉女素心剑法,全真剑法,蛤蟆功</item>
<item>玉女素心剑法,古墓派轻功,金铃锁、淑女剑、玉蜂针、白金丝掌套</item>
</string-array>
</resources>
//dimens.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="min_Height">64dip</dimen>
<dimen name="p_w_picpath_width">60dip</dimen>
</resources>
由于demo图片过多,导致资源超过2M无法上传
留下地址,免费下载哦!!!
http://down.51cto.com/data/1888072
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。