实现的效果如下:



用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"?>            

下面是buddy_listview_child_item.xml

<?xml version="1.0" encoding="utf-8"?>            


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 stubreturn true;}}



更多相关文章

  1. ionic android app 签名处理
  2. 在Android(安卓)Studio中使用androidannotations(安卓注解)的方法(
  3. android-项目结构详解
  4. Android自定义属性,attr format取值类型
  5. Android(安卓)获取LocationProvider以及获取定位信息
  6. android 动态增加不同名称的按钮
  7. android 获取Service(服务)的运行状态
  8. Android用类继承Application的全局变量使用注意
  9. Android设备指纹大全

随机推荐

  1. RxCache--打造自己的Android缓存框架
  2. Android(安卓)adb远程调试
  3. Android点击菜单之后跳转到一个新的页面
  4. 利用power_profile.xml获取Android运行时
  5. FFmpeg 音频编码(PCM数据编码成AAC androi
  6. android 跳转时出现短暂的黑屏继承AppCom
  7. Android(安卓)Studio 1.2正式版发布,要如
  8. Android(安卓)开发中的日常积累
  9. Android(安卓)面试题总结之View(一)
  10. Android强制弹出,隐藏输入法.