android 可展開(收縮)的列表ListView(ExpandableListView)

來源:互聯網
上載者:User

每天都在用QQ聊天,今天突然一想,android怎麼實現列表的分組展開呢?看了看api,發現其實現過程也很簡單。先看一下最終效果吧!

 

1、首先建立我們的Activity,繼承 android.app.ExpandableListActivity,直接看代碼吧。

package com.ideasandroid.sample;import android.app.ExpandableListActivity;import android.os.Bundle;import com.ideasandroid.sample.adapter.IdeasExpandableListAdapter; /** * @author IdeasAndroid * 可展開(收縮)列表示例 */public class IdeasExpandableListView extends ExpandableListActivity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //設定列表適配器IdeasExpandableListAdapter        setListAdapter(new IdeasExpandableListAdapter(this));    }}

2、建立適配器,繼承android.widget.BaseExpandableListAdapter。

package com.ideasandroid.sample.adapter; import java.util.ArrayList;import java.util.List;import android.content.Context;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.TextView; /** * @author IdeasAndroid  * 可展開(收縮)列表示例 */public class IdeasExpandableListAdapter extends BaseExpandableListAdapter {         private Context mContext = null;        // 測試資料,開發時可能來自資料庫,網路....        private String[] groups = { "家人", "朋友", "同事" };        private String[] familis = { "老爸", "老媽", "妹妹" };        private String[] friends = { "小李", "張三", "李四" };        private String[] colleagues = { "陳總", "李工", "李客戶" };         private List<String> groupList = null;        private List<List<String>> itemList = null;         public IdeasExpandableListAdapter(Context context) {                this.mContext = context;                groupList = new ArrayList<String>();                itemList = new ArrayList<List<String>>();                initData();        }         /**         * 初始化資料,將相關資料放到List中,方便處理         */        private void initData() {                for (int i = 0; i < groups.length; i++) {                        groupList.add(groups[i]);                }                List<String> item1 = new ArrayList<String>();                for (int i = 0; i < familis.length; i++) {                        item1.add(familis[i]);                }                 List<String> item2 = new ArrayList<String>();                for (int i = 0; i < friends.length; i++) {                        item2.add(friends[i]);                }                 List<String> item3 = new ArrayList<String>();                for (int i = 0; i < colleagues.length; i++) {                        item3.add(colleagues[i]);                }                 itemList.add(item1);                itemList.add(item2);                itemList.add(item3);        }         public boolean areAllItemsEnabled() {                return false;        }         /*         * 設定子節點對象,在事件處理時返回的對象,可存放一些資料         */        public Object getChild(int groupPosition, int childPosition) {                return itemList.get(groupPosition).get(childPosition);        }         public long getChildId(int groupPosition, int childPosition) {                return childPosition;        }         /*         * 位元組點視圖,這裡我們顯示一個文字物件         */        public View getChildView(int groupPosition, int childPosition,                        boolean isLastChild, View convertView, ViewGroup parent) {                TextView text = null;                if (convertView == null) {                        text = new TextView(mContext);                } else {                        text = (TextView) convertView;                }                // 擷取子節點要顯示的名稱                String name = (String) itemList.get(groupPosition).get(childPosition);                // 設定文本視圖的相關屬性                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(                                ViewGroup.LayoutParams.FILL_PARENT, 40);                text.setLayoutParams(lp);                text.setTextSize(18);                text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);                text.setPadding(45, 0, 0, 0);                text.setText(name);                return text;        }         /*         * 返回當前分組的位元組點個數         */        public int getChildrenCount(int groupPosition) {                return itemList.get(groupPosition).size();        }         /*         * 返回分組對象,用於一些資料傳遞,在事件處理時可直接取得和分組相關的資料         */        public Object getGroup(int groupPosition) {                return groupList.get(groupPosition);        }         /*         * 分組的個數         */        public int getGroupCount() {                return groupList.size();        }         public long getGroupId(int groupPosition) {                return groupPosition;        }         /*         * 分組視圖,這裡也是一個文本視圖         */        public View getGroupView(int groupPosition, boolean isExpanded,                        View convertView, ViewGroup parent) {                TextView text = null;                if (convertView == null) {                        text = new TextView(mContext);                } else {                        text = (TextView) convertView;                }                String name = (String) groupList.get(groupPosition);                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(                                ViewGroup.LayoutParams.FILL_PARENT, 40);                text.setLayoutParams(lp);                text.setTextSize(18);                text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);                text.setPadding(36, 0, 0, 0);                text.setText(name);                return text;        }         /*         * 判斷分組是否為空白,本樣本中資料是固定的,所以不會為空白,我們返回false          * 如果資料來自資料庫,網路時,可以把判斷邏輯寫到這個方法中,如果為空白         * 時返回true         */        public boolean isEmpty() {                return false;        }         /*         * 收縮列表時要處理的東西都放這兒         */        public void onGroupCollapsed(int groupPosition) {         }         /*         * 展開列表時要處理的東西都放這兒         */        public void onGroupExpanded(int groupPosition) {         }         /*         * Indicates whether the child and group IDs are stable across changes to         * the underlying data.         */        public boolean hasStableIds() {                return false;        }         /*         * Whether the child at the specified position is selectable.         */        public boolean isChildSelectable(int groupPosition, int childPosition) {                return true;        }}

IdeasExpandableListView.rar

轉:http://www.apkbus.com/android-54271-1-1.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.