Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android ExpandableListView 詳解:打造類似 QQ 好友分組列表

編輯:關於 Android 程式設計

由於工作需要,今天簡單研究了一下 ExpandableListView,做了一個類似 QQ 好友列表的 Demo,在此與大家分享。

效果圖如下:

先來看主佈局文件:


    
    

這裡我們不使用系統默認的分隔線,讓分組之間保持 8dp 的間距。

對於 ExpandableListView,系統提供了專門的適配器基類 BaseExpandableListAdapter,我們自定義一個適配器繼承它,實現相關方法即可。代碼如下:

public class MyAdapter extends BaseExpandableListAdapter {
    private List list;
    private Context context;
    public MyAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }
    public MyAdapter() { }
    @Override public int getGroupCount() { return list.size(); }
    @Override public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getChildren().size();
    }
    @Override public Object getGroup(int groupPosition) { return list.get(groupPosition); }
    @Override public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getChildren().get(childPosition);
    }
    @Override public long getGroupId(int groupPosition) { return groupPosition; }
    @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; }
    @Override public boolean hasStableIds() { return false; }
    @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        if (convertView == null) {
            holder = new GroupHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group, null);
            holder.title = (TextView) convertView.findViewById(R.id.group_title);
            holder.iv = (ImageView) convertView.findViewById(R.id.group_ico);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.title.setText(list.get(groupPosition).getGroupName());
        if (isExpanded) {
            holder.iv.setImageResource(R.drawable.rounds_open);
        } else {
            holder.iv.setImageResource(R.drawable.rounds_close);
        }
        return convertView;
    }
    @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        if (convertView == null) {
            holder = new ChildHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_child, null);
            holder.name = (TextView) convertView.findViewById(R.id.child_name);
            holder.sign = (TextView) convertView.findViewById(R.id.child_sign);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        ChildBean cb = list.get(groupPosition).getChildren().get(childPosition);
        holder.name.setText(cb.getName());
        holder.sign.setText("【簽名】" + cb.getSign());
        return convertView;
    }
    @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; }
    class GroupHolder { TextView title; ImageView iv; }
    class ChildHolder { TextView name, sign; }
}

這段代碼稍長,我們稍微解釋一下。構造方法中傳入兩個參數:數據源 list 與上下文 context。其中 list 是 GroupBean 集合,GroupBean 結構如下:

public class GroupBean {
    private String groupName;
    private List children;
    public String getGroupName() { return groupName; }
    public void setGroupName(String groupName) { this.groupName = groupName; }
    public List getChildren() { return children; }
    public void setChildren(List children) { this.children = children; }
    public GroupBean(String groupName, List children) {
        this.groupName = groupName;
        this.children = children;
    }
    public GroupBean() { }
}

可以看到,GroupBean 有兩個屬性:分組名稱,以及該組下子項的集合。ChildBean 則代表每個組下每一項的數據,代碼如下:

public class ChildBean {
    private String name;
    private String sign;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getSign() { return sign; }
    public void setSign(String sign) { this.sign = sign; }
    public ChildBean(String name, String sign) {
        this.name = name;
        this.sign = sign;
    }
    public ChildBean() { }
}

說完數據結構,再看 getGroupCount() 與 getChildrenCount(),它們有點類似於 BaseAdapter 中的 getCount(),不同的是這裡要分別返回分組數量與組內成員數量。分組數量即 list.size();由於每組成員數量不同,需先取到該組再取成員數,即 list.get(groupPosition).getChildren().size();。接下來的 getGroup() 與 getChild() 類似 BaseAdapter 的 getItem(),只是分組與子項分開返回。稍長的 getGroupView 與 getChildView 也沒什麼邏輯,和 ListView 中 getView 的用法相近,僅在數據賦值上有差別。

個人體會:使用 ExpandableListView 的關鍵在於理清數據結構,把 Group 和 Child 搞明白了,其餘都很簡單。

下面把 Group 與 Child 的佈局貼出來:

item_group.xml


    
        
            
            
        
    

item_child.xml


    
        
            
            
        
    

這裡還用到了一個圓角方框背景,代碼如下:

item_background_select.xml


    
        
        
    

所有準備工作完成後,來看如何使用:

public class MainActivity extends Activity {
    private ExpandableListView mListView;
    private MyAdapter adapter;
    private List list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化數據
        initData();
        mListView = (ExpandableListView) this.findViewById(R.id.my_listview);
        adapter = new MyAdapter(list, this);
        mListView.setAdapter(adapter);
        mListView.setGroupIndicator(null);
        // mListView.expandGroup(0);
    }
    private void initData() {
        list = new ArrayList();
        { /* 家庭分組 */ }
        { /* 朋友分組 */ }
        { /* 國際友人在此省略具體數據 */ }
        { /* 同事分組 */ }
    }
}

這裡有兩行代碼需要說明:mListView.setGroupIndicator(null); 表示不使用系統默認的展開/收起圖標;mListView.expandGroup(0); 表示默認展開第一項。

以上就是全部內容,如有問題歡迎留言討論。

 

版權聲明:本文為原創文章,未經允許請勿轉載。若有錯誤之處,歡迎批評指正。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved