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

android 实现QQ好友列表(扩展listview:ExpandableListView)


        在某些android开发群里,看到有些新手问怎么实现QQ好友列表,其实网上一搜挺多的。接触Android,也才一年的时间,大部分时间花在工作上(解bug。。。),界面上开发很少参与。自己维护的系统应用里,有个ExpandableListView的界面(其实android例子APIDemo也有类似的例子)就在这里写个Demo供新手参考。

        ExpandableListView的用法:难点就是重写BaseExpandableListAdapter及提供的数据源。

        下面看看继承BaseExpandableListAdapter的适配器:


[java]
<span xmlns="http://www.w3.org/1999/xhtml" style="">package com.xyz.expande; 
 
import java.util.List; 
 
import android.app.AlertDialog; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 
 
public class ExpandAdapter extends BaseExpandableListAdapter { 
 
    private Context mContext; 
    private LayoutInflater mInflater = null; 
    private String[]   mGroupStrings = null; 
    private List<List<Item>>   mData = null; 
 
    public ExpandAdapter(Context ctx, List<List<Item>> list) { 
        mContext = ctx; 
        mInflater = (LayoutInflater) mContext 
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        mGroupStrings = mContext.getResources().getStringArray(R.array.groups); 
        mData = list; 
    } 
 
    public void setData(List<List<Item>> list) { 
        mData = list; 
    } 
 
    @Override 
    public int getGroupCount() { 
        // TODO Auto-generated method stub 
        return mData.size(); 
    } 
 
    @Override 
    public int getChildrenCount(int groupPosition) { 
        // TODO Auto-generated method stub 
        return mData.get(groupPosition).size(); 
    } 
 
    @Override 
    public List<Item> getGroup(int groupPosition) { 
        // TODO Auto-generated method stub 
        return mData.get(groupPosition); 
    } 
 
    @Override 
    public Item getChild(int groupPosition, int childPosition) { 
        // TODO Auto-generated method stub 
        return mData.get(groupPosition).get(childPosition); 
    } 
 
    @Override 
    public long getGroupId(int groupPosition) { 
        // TODO Auto-generated method stub 
        return groupPosition; 
    } 
 
    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
        // TODO Auto-generated method stub 
        return childPosition; 
    } 
 
    @Override 
    public boolean hasStableIds() { 
        // TODO Auto-generated method stub 
        return false; 
    } 
 
    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
            View convertView, ViewGroup parent) { 
        // TODO Auto-generated method stub 
        if (convertView == null) { 
            convertView = mInflater.inflate(R.layout.group_item_layout, null); 
        } 
        GroupViewHolder holder = new GroupViewHolder(); 
        holder.mGroupName = (TextView) convertView 
                .findViewById(R.id.group_name); 
        holder.mGroupName.setText(mGroupStrings[groupPosition]); 
        holder.mGroupCount = (TextView) convertView 
                .findViewById(R.id.group_count); 
        holder.mGroupCount.setText("[" + mData.get(groupPosition).size() + "]"); 
        return convertView; 
    } 
 
    @Override 
    public View getChildView(int groupPosition, int childPosition, 
            boolean isLastChild, View convertView, ViewGroup parent) { 
        // TODO Auto-generated method stub 
        if (convertView == null) { 
            convertView = mInflater.inflate(R.layout.child_item_layout, null); 
        } 
        ChildViewHolder holder = new ChildViewHolder(); 
        holder.mIcon = (ImageView) convertView.findViewById(R.id.img); 
        holder.mIcon.setBackgroundResource(getChild(groupPosition, 
                childPosition).getImageId()); 
        holder.mChildName = (TextView) convertView.findViewById(R.id.item_name); 
        holder.mChildName.setText(getChild(groupPosition, childPosition) 
                .getName()); 
        holder.mDetail = (TextView) convertView.findViewById(R.id.item_detail); 
  &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,