热门IT资讯网

ExpandableListQQ好友列表

发表于:2024-11-25 作者:热门IT资讯网编辑
编辑最后更新 2024年11月25日,类似于腾讯的QQ好友列表package com.example.testexpandablelist;import java.util.List;import android.app.Activity

类似于腾讯的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 groupArray;

private List> childArray;


private MyExpandableAdapter mAdapter;


private List> data = null;

private List> 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 list = new ArrayList();

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


16dp

16dp

64dip

60dip



由于demo图片过多,导致资源超过2M无法上传

留下地址,免费下载哦!!!

http://down.51cto.com/data/1888072


0