Android 高仿QQ 好友分组列表

实现的效果如下:



用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



    
    
    

下面是buddy_listview_child_item.xml



    
    
    


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) {
		// TODO Auto-generated method stub
		return true;
	}
}



你可能感兴趣的:(Android)