1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| public class MainActivity extends Activity { private LayoutInflater mInflater; private IphoneTreeView iphoneTreeView;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); }
private void initView() { mInflater = LayoutInflater.from(this); iphoneTreeView = (IphoneTreeView) findViewById(R.id.iphone_tree_view); iphoneTreeView.setHeaderView(getLayoutInflater().inflate( R.layout.list_head_view, iphoneTreeView, false)); iphoneTreeView.setGroupIndicator(null); iphoneTreeView.setAdapter(new IphoneTreeViewAdapter()); }
public class IphoneTreeViewAdapter extends BaseExpandableListAdapter implements IphoneTreeHeaderAdapter { private HashMap<Integer, Integer> groupStatusMap; private String[] groups = { "第一组", "第二组", "第三组", "第四组" }; private String[][] children = { { "Way", "Arnold", "Barry", "Chuck", "David", "Afghanistan", "Albania", "Belgium", "Lily", "Jim", "LiMing", "Jodan" }, { "Ace", "Bandit", "Cha-Cha", "Deuce", "Bahamas", "China", "Dominica", "Jim", "LiMing", "Jodan" }, { "Fluffy", "Snuggles", "Ecuador", "Ecuador", "Jim", "LiMing", "Jodan" }, { "Goldy", "Bubbles", "Iceland", "Iran", "Italy", "Jim", "LiMing", "Jodan" } };
public IphoneTreeViewAdapter() { groupStatusMap = new HashMap<Integer, Integer>(); }
public Object getChild(int groupPosition, int childPosition) { return children[groupPosition][childPosition]; }
public long getChildId(int groupPosition, int childPosition) { return childPosition; }
public int getChildrenCount(int groupPosition) { return children[groupPosition].length; }
public Object getGroup(int groupPosition) { return groups[groupPosition]; }
public int getGroupCount() { return groups.length; }
public long getGroupId(int groupPosition) { return groupPosition; }
public boolean isChildSelectable(int groupPosition, int childPosition) { return true; }
public boolean hasStableIds() { return true; }
@Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item_view, null); } TextView tv = (TextView) convertView .findViewById(R.id.contact_list_item_name); tv.setText(getChild(groupPosition, childPosition).toString()); TextView state = (TextView) convertView .findViewById(R.id.cpntact_list_item_state); state.setText("爱生活...爱Android..."); return convertView; }
@Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.list_group_view, null); } TextView groupName = (TextView) convertView .findViewById(R.id.group_name); groupName.setText(groups[groupPosition]);
ImageView indicator = (ImageView) convertView .findViewById(R.id.group_indicator); TextView onlineNum = (TextView) convertView .findViewById(R.id.online_count); onlineNum.setText(getChildrenCount(groupPosition) + "/" + getChildrenCount(groupPosition)); if (isExpanded) { indicator.setImageResource(R.drawable.indicator_expanded); } else { indicator.setImageResource(R.drawable.indicator_unexpanded); } return convertView; }
@Override public int getTreeHeaderState(int groupPosition, int childPosition) { final int childCount = getChildrenCount(groupPosition); if (childPosition == childCount - 1) { return PINNED_HEADER_PUSHED_UP; } else if (childPosition == -1 && !iphoneTreeView.isGroupExpanded(groupPosition)) { return PINNED_HEADER_GONE; } else { return PINNED_HEADER_VISIBLE; } }
@Override public void configureTreeHeader(View header, int groupPosition, int childPosition, int alpha) { ((TextView) header.findViewById(R.id.group_name)) .setText(groups[groupPosition]); ((TextView) header.findViewById(R.id.online_count)) .setText(getChildrenCount(groupPosition) + "/" + getChildrenCount(groupPosition)); }
@Override public void onHeadViewClick(int groupPosition, int status) { groupStatusMap.put(groupPosition, status); }
@Override public int getHeadViewClickStatus(int groupPosition) { if (groupStatusMap.containsKey(groupPosition)) { return groupStatusMap.get(groupPosition); } else { return 0; } }
} }
|