实现的效果如下:
用ExpandableListView实现, 先看Activity的代码:
- public class BuddyActivity extends Activity {
- ExpandableListView expandablelistview;
-
- private String[] group = new String[] { "在线好友", "我的好友", "我的同事"};
-
- private String[][] buddy = new String[][] {
- { "元芳", "雷丶小贱", "狄大人"},
- {"高太后", "士兵甲", "士兵乙", "士兵丙" },
- { "艺术家", "叫兽", "攻城师", "职业玩家" }};
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_buddy);
-
- expandablelistview= (ExpandableListView) findViewById(R.id.buddy_expandablelistview);
- ExpandableListAdapter adapter=new BuddyAdapter(this,group,buddy);
- expandablelistview.setAdapter(adapter);
-
- expandablelistview.setOnGroupExpandListener(new OnGroupExpandListener(){
- public void onGroupExpand(int groupPosition) {
- }
- });
-
- expandablelistview.setOnGroupCollapseListener(new OnGroupCollapseListener(){
- public void onGroupCollapse(int groupPosition) {
- }
- });
-
- expandablelistview.setOnChildClickListener(new OnChildClickListener(){
- public boolean onChildClick(ExpandableListView arg0, View arg1,
- int groupPosition, int childPosition, long arg4) {
- Toast.makeText(BuddyActivity.this,
- group[groupPosition]+" : "+buddy[groupPosition][childPosition],
- Toast.LENGTH_SHORT).show();
- return false;
- }
- });
- }
- }
ExpandableListView的布局有2个,分别是组的布局和子项(即好友),先看buddy_listview_group_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RelativeLayout1"
- android:layout_width="match_parent"
- android:layout_height="30dip"
- android:background="@drawable/group_fold_bg">
- <ImageView
- android:id="@+id/buddy_listview_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="10dp"
- android:src="@drawable/group_fold_arrow" />
- <TextView
- android:id="@+id/buddy_listview_group_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_alignTop="@+id/buddy_listview_image"
- android:layout_toRightOf="@+id/buddy_listview_image"
- android:text="分组名称" />
- <TextView
- android:id="@+id/buddy_listview_group_num"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:layout_marginRight="10dp"
- android:text="[10/10]" />
- </RelativeLayout>
下面是buddy_listview_child_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="40dip" >
- <ImageView
- android:id="@+id/buddy_listview_child_avatar"
- android:layout_width="40dip"
- android:layout_height="40dip"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:layout_marginLeft="10dp"
- android:src="@drawable/avatar" />
- <TextView
- android:id="@+id/buddy_listview_child_nick"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_toRightOf="@+id/buddy_listview_child_avatar"
- android:text="昵称"
- android:textSize="16sp" />
- <TextView
- android:id="@+id/buddy_listview_child_trends"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/buddy_listview_child_nick"
- android:layout_alignParentBottom="true"
- android:text="该好友暂时没有新动态"
- android:textSize="12sp" />
-
- </RelativeLayout>
Adapter自然是对应的BaseExpandableListAdapter
- public class BuddyAdapter extends BaseExpandableListAdapter {
- private String[] group;
- private String[][] buddy;
- private Context context;
- LayoutInflater inflater;
-
- public BuddyAdapter(Context context,String[] group,String[][] buddy){
- this.context=context;
- inflater = LayoutInflater.from(context);
- this.group=group;
- this.buddy=buddy;
- }
- public Object getChild(int groupPosition, int childPosition) {
- return buddy[groupPosition][childPosition];
- }
-
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
-
- public View getChildView(int groupPosition, int childPosition, boolean arg2, View convertView,
- ViewGroup arg4) {
- convertView = inflater.inflate(R.layout.buddy_listview_child_item, null);
- TextView nickTextView=(TextView) convertView.findViewById(R.id.buddy_listview_child_nick);
- nickTextView.setText(getChild(groupPosition, childPosition).toString());
- return convertView;
- }
-
- public int getChildrenCount(int groupPosition) {
- return buddy[groupPosition].length;
- }
-
- public Object getGroup(int groupPosition) {
- return group[groupPosition];
- }
-
- public int getGroupCount() {
- return group.length;
- }
-
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
-
- public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup arg3) {
- convertView = inflater.inflate(R.layout.buddy_listview_group_item, null);
- TextView groupNameTextView=(TextView) convertView.findViewById(R.id.buddy_listview_group_name);
- groupNameTextView.setText(getGroup(groupPosition).toString());
- ImageView image = (ImageView) convertView.findViewById(R.id.buddy_listview_image);
- image.setImageResource(R.drawable.group_unfold_arrow);
-
- if(!isExpanded){
- image.setImageResource(R.drawable.group_fold_arrow);
- }
- return convertView;
- }
-
- public boolean hasStableIds() {
- return true;
- }
-
- public boolean isChildSelectable(int arg0, int arg1) {
-
- return true;
- }
- }
|