您好,登錄后才能下訂單哦!
類似于騰訊的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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。