ExpandableListQQ好友列表
类似于腾讯的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> mData = null;
private Context mContext;
private LayoutInflater mInflater;
private String[] mGroupStrings;
public MyExpandableAdapter(Context context, List> 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
private List> childArray;
private MyExpandableAdapter mAdapter;
private List
private List
private List> mData = new ArrayList
>();
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 List String[] childs = getStringArray(mGroupArrays[i]); String[] details = getStringArray(mDetailIds[i]); for(int j=0; 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 //dimens.xml 由于demo图片过多,导致资源超过2M无法上传 留下地址,免费下载哦!!!http://down.51cto.com/data/1888072