首先是数据泵的绑定包括group和child的双赋值帮定
public class DesktopAdapter extends BaseExpandableListAdapter {
private Context mContext = null;
private List<Map<String, Object>> mGroup = null;
private List<List<Map<String, Object>>> mChild = null;
private LayoutInflater mInflater = null;
public DesktopAdapter(Context context, List<Map<String, Object>> group,
List<List<Map<String, Object>>> child) {
mContext = context;
mGroup = group;
mChild = child;
mInflater = LayoutInflater.from(mContext);
}
public Object getChild(int groupPosition, int childPosition) {
return mChild.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.desktop_list_child, null);
holder = new ViewHolder();
holder.mChildIcon = (ImageView) convertView
.findViewById(R.id.desktop_list_child_icon);
holder.mChildName = (TextView) convertView
.findViewById(R.id.desktop_list_child_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mChildIcon.setImageDrawable(mContext.getResources().getDrawable(
Integer.parseInt(mChild.get(groupPosition).get(childPosition)
.get("icon").toString())));
holder.mChildName.setText(mChild.get(groupPosition).get(childPosition)
.get("name").toString());
if (childPosition == Desktop.mChooesId && groupPosition == 0) {
convertView
.setBackgroundResource(R.drawable.desktop_list_item_pressed);
} else {
convertView.setBackgroundResource(R.drawable.desktop_list_item_bg);
}
return convertView;
}
public int getChildrenCount(int groupPosition) {
return mChild.get(groupPosition).size();
}
public Object getGroup(int groupPosition) {
return mGroup.get(groupPosition);
}
public int getGroupCount() {
return mGroup.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//分组布局的引入
convertView = mInflater.inflate(R.layout.desktop_list_group, null);
holder = new ViewHolder();
holder.mGroupName = (TextView) convertView
.findViewById(R.id.desktop_list_group_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mGroupName.setText(mGroup.get(groupPosition).get("name")
.toString());
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class ViewHolder {
private TextView mGroupName;
private ImageView mChildIcon;
private TextView mChildName;
}
}
chil的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/desktop_list_child_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip" />
<TextView
android:id="@+id/desktop_list_child_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:textColor="#ffd6d6d6"
android:textSize="18sp" />
</LinearLayout>
group父布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical">
<!-- 组分类的名字 -->
<TextView
android:id="@+id/desktop_list_group_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff888888"
android:textSize="12sp"
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"
android:layout_marginLeft="15dip"/>
<!-- 分割条 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#30ffffff"
android:layout_marginLeft="15dip">
</LinearLayout>
</LinearLayout>
剩下来就是调用和赋值
public class Desktop {
private BaseApplication mApplication;
private Context mContext;
private View mDesktop;
private LinearLayout mInformation;
private ImageView mAvatar;
private TextView mName;
private ExpandableListView mDisplay;
private List<Map<String, Object>> mGroup = new ArrayList<Map<String, Object>>();
private List<List<Map<String, Object>>> mChild = new ArrayList<List<Map<String, Object>>>();
private String[] mGroupName;
private String[] mChildFavorite;
private int[] mChildFavoriteIcon;
private String[] mChildAction;
private int[] mChildActionIcon;
public static int mChooesId = 0;
private DesktopAdapter mAdapter;
private onChangeViewListener mOnChangeViewListener;
public Desktop(BaseApplication application, Context context) {
mApplication = application;
mContext = context;
mDesktop = LayoutInflater.from(context).inflate(R.layout.desktop, null);
findViewById();
init();
setListener();
}
private void findViewById() {
mInformation = (LinearLayout) mDesktop
.findViewById(R.id.desktop_top_layout);
mAvatar = (ImageView) mDesktop.findViewById(R.id.desktop_top_avatar);
mName = (TextView) mDesktop.findViewById(R.id.desktop_top_name);
mDisplay = (ExpandableListView) mDesktop
.findViewById(R.id.desktop_list);
}
private void setListener() {
mInformation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mChooesId = -1;
mAdapter.notifyDataSetChanged();
if (mOnChangeViewListener != null) {
mOnChangeViewListener.onChangeView(View_Util.Information);
}
}
});
mDisplay.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return true;
}
});
mDisplay.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
mChooesId = childPosition;
mAdapter.notifyDataSetChanged();
if (mOnChangeViewListener != null) {
switch (childPosition) {
case 0:
mOnChangeViewListener
.onChangeView(View_Util.NewsFeed);
break;
case 1:
mOnChangeViewListener
.onChangeView(View_Util.Message);
break;
case 2:
mOnChangeViewListener.onChangeView(View_Util.Chat);
break;
case 3:
mOnChangeViewListener
.onChangeView(View_Util.Friends);
break;
case 4:
mOnChangeViewListener.onChangeView(View_Util.Page);
break;
case 5:
mOnChangeViewListener
.onChangeView(View_Util.Location);
break;
case 6:
mOnChangeViewListener
.onChangeView(View_Util.Search);
break;
case 7:
mOnChangeViewListener
.onChangeView(View_Util.Apps_Center);
break;
}
}
} else {
switch (childPosition) {
case 0:
mContext.startActivity(new Intent(mContext,
Settings.class));
break;
case 1:
Toast.makeText(mContext, "因无登录界面,所以暂时无操作",
Toast.LENGTH_SHORT).show();
break;
}
}
return true;
}
});
}
private void init() {
init_Data();
mName.setText(mApplication.mRenRen.getUserName());
mApplication.mHeadBitmap.display(mAvatar,
mApplication.mRenRen.getUserHeadUrl());
mAdapter = new DesktopAdapter(mContext, mGroup, mChild);
mDisplay.setAdapter(mAdapter);
for (int i = 0; i < mGroup.size(); i++) {
mDisplay.expandGroup(i);
}
}
private void init_Data() {
//从数组中获取对应的
mGroupName = mContext.getResources().getStringArray(
R.array.desktop_list_head_strings);
mChildFavorite = mContext.getResources().getStringArray(
R.array.desktop_list_item_favorite_strings);
mChildAction = mContext.getResources().getStringArray(
R.array.desktop_list_item_action_strings);
//获取图片
mChildFavoriteIcon = new int[8];
mChildActionIcon = new int[2];
mChildFavoriteIcon[0] = R.drawable.v5_0_1_desktop_list_newsfeed;
mChildFavoriteIcon[1] = R.drawable.v5_0_1_desktop_list_message;
mChildFavoriteIcon[2] = R.drawable.v5_0_1_desktop_list_chat;
mChildFavoriteIcon[3] = R.drawable.v5_0_1_desktop_list_friends;
mChildFavoriteIcon[4] = R.drawable.v5_0_1_desktop_list_page;
mChildFavoriteIcon[5] = R.drawable.v5_0_1_desktop_list_location;
mChildFavoriteIcon[6] = R.drawable.v5_0_1_desktop_list_search;
mChildFavoriteIcon[7] = R.drawable.v5_0_1_desktop_list_apps_center;
mChildActionIcon[0] = R.drawable.v5_0_1_desktop_list_settings;
mChildActionIcon[1] = R.drawable.v5_0_1_desktop_list_log_out;
getGroupList();
getChildList();
}
private void getGroupList() {
for (int i = 0; i < mGroupName.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", mGroupName[i]);
mGroup.add(map);
}
}
private void getChildList() {
for (int i = 0; i < mGroupName.length; i++) {
if (i == 0) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int j = 0; j < mChildFavorite.length; j++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("icon", mChildFavoriteIcon[j]);
map.put("name", mChildFavorite[j]);
map.put("click", false);
list.add(map);
}
mChild.add(list);
} else if (i == 1) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int j = 0; j < mChildAction.length; j++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("icon", mChildActionIcon[j]);
map.put("name", mChildAction[j]);
map.put("click", false);
list.add(map);
}
mChild.add(list);
}
}
// 默认选择常用组第一项
Map<String, Object> map = new HashMap<String, Object>();
map.put("icon", mChildFavoriteIcon[0]);
map.put("name", mChildFavorite[0]);
map.put("click", true);
mChild.get(0).set(0, map);
}
public View getView() {
return mDesktop;
}
public interface onChangeViewListener {
public abstract void onChangeView(int arg0);
}
public void setOnChangeViewListener(
onChangeViewListener onChangeViewListener) {
mOnChangeViewListener = onChangeViewListener;
}
}