ExpandableListView總結(jié)-創(chuàng)新互聯(lián)

在實(shí)現(xiàn)類似通信錄,等帶有兩層或多層組織架構(gòu)的列表功能時(shí),會(huì)使用到ExpandableListView,他是ListView的子類,使用方式也和ListView大同小異,這里做一個(gè)總結(jié)。

創(chuàng)新互聯(lián)公司長(zhǎng)期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為路北企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,路北網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

關(guān)鍵類:

1、SimpleExpandableListAdapter

2、SimpleCursorTreeAdapter

3、BaseExpandableListAdapter

4、ExpandableListActivity

SimpleExpandableListAdapter

構(gòu)造方法:

SimpleExpandableListAdapter(Context context,

List<? extends Map<String, ?>> groupData, //組數(shù)據(jù)

int groupLayout,//組數(shù)據(jù)使用的布局文件

String[] groupFrom, //組數(shù)據(jù)所使用的key數(shù)組

int[] groupTo,//組數(shù)據(jù)放置的控件id,例如TextView

List<? extends List<? extends Map<String,?>>> childData,//子元素?cái)?shù)據(jù)

int childLayout, //子元素使用的布局文件

String[] childFrom, //子元素?cái)?shù)據(jù)所使用的key數(shù)組

int[] childTo)//子元素?cái)?shù)據(jù)放置的控件id,例如TextView

例如:生成一條組數(shù)據(jù),組名為“group1”

Map<String, String> map1 = new HashMap<>();

map1.put("group", "group1");//其中"group"只是一個(gè)索引key

集合保存所有的組數(shù)據(jù)

List<Map<String, String>> groups = new ArrayList<>();

保存一條子元素?cái)?shù)據(jù),和組數(shù)據(jù)一樣

Map<String, String> cMap1 = new HashMap<>();

cMap1.put("child","child1_data1");

child1.add(cMap1);//添加到集合

保存一個(gè)組的子元素?cái)?shù)據(jù)

List<Map<String, String>> child1 = new ArrayList<>();

保存所有組的子元素?cái)?shù)據(jù)

List<List<Map<String, String>>> childs = new ArrayList<>();

構(gòu)造方法

SimpleExpandableListAdapter adapter1 = new SimpleExpandableListAdapter(this,

groups, R.layout.expandable_group, new String[]{"group"}, new int[]{R.id.text_group},

childs, R.layout.expandable_child, new String[]{"child"}, new int[]{R.id.text_child});

BaseExpandableListAdapter

繼承BaseExpandableListAdapter實(shí)現(xiàn)自己的Adapter:

public class MyAdapter extends BaseExpandableListAdapter{

    String[] groups = new String[]{"Group_111","Group_222","Group_333"};
    String[][] childs = new String[][]{
            {"g1_c1","g1_c2","g1_c3"},
            {"g2_c1","g2_c2","g2_c3"},
            {"g3_c1","g3_c2","g3_c3"}};

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childs[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;//這里直接返回組元素下標(biāo)
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;//這里直接返回第二層元素下標(biāo)
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = View.inflate(MainActivity.this, R.layout.expandable_group, null);
        TextView text = (TextView)view.findViewById(R.id.text_group);
        text.setText(groups[groupPosition]);
        return view;//實(shí)際項(xiàng)目中應(yīng)該考慮convertView的重用
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = View.inflate(MainActivity.this, R.layout.expandable_child, null);
        TextView text = (TextView)view.findViewById(R.id.text_child);
        text.setText(childs[groupPosition][childPosition]);
        return view;
    }

    @Override//組合子元素是否擁有穩(wěn)定的id,底層的數(shù)據(jù)集改表是否影響該id
    public boolean hasStableIds() {
        return false;
    }

    @Override//子元素是否可選中,有點(diǎn)擊反饋
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

ExpandableListActivity和ListActivity類似

  • 寫自己的Activity繼承自該類

  • 該類自帶一個(gè)ExpandableListView。id為:“@android:id/list”(或者代碼中為“l(fā)ist”)

  • 可以有一個(gè)id為:“@android:id/empty”布局,作為adapter沒(méi)數(shù)據(jù)時(shí)的替代布局

  • 通常寫自己的布局并滿足上述條件,并加以擴(kuò)展實(shí)現(xiàn)自己的需求

SimpleCursorTreeAdapter

  不同于SimpleExpandableListAdapter數(shù)據(jù)源來(lái)自List<Map<?,?>>,SimpleCursorTreeAdapter數(shù)據(jù)源來(lái)自訪問(wèn)數(shù)據(jù)庫(kù)返回的Cursor。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

本文標(biāo)題:ExpandableListView總結(jié)-創(chuàng)新互聯(lián)
標(biāo)題URL:http://bm7419.com/article28/cecicp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站制作、響應(yīng)式網(wǎng)站、關(guān)鍵詞優(yōu)化Google、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)