当前位置:操作系统 > 安卓/Android >>

Android 高仿QQ 好友分组列表

实现的效果如下:

 \

 

 

用ExpandableListView实现,

先看Activity的代码:


[java] 
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; 
            } 
        }); 
    } 

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

 

[html] 
<?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"> 补充:移动开发 , Android ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,