ExpandableListView的簡(jiǎn)單實(shí)踐

參考自Android小白
http://blog.csdn.net/sysukehan/article/details/51960473

MainActivity 的布局 layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Hello World!"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/expandlistview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:id="@+id/updateData"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:textSize="20dp"
android:layout_gravity="center"
android:text="刷新數(shù)據(jù)"/>
</LinearLayout>
</LinearLayout>

<!--需要使分割線消失的話仇让,不要將這兩個(gè)屬性設(shè)置為@null典奉,-->
<!--把它們的顏色設(shè)置為與背景顏色相同的顏色即可。-->
<!--如果想要用自己的圖片替換掉那個(gè)箭頭可以這樣寫
 android:groupIndicator="@drawable/picture"
    android:groupIndicator="@null"
    childIndicator
 用于設(shè)置子項(xiàng)前顯示的圖標(biāo)丧叽,不設(shè)置的話默認(rèn)是沒有圖標(biāo)的
 -->

父項(xiàng)的布局文件 layout/parent_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/parent_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

子類的布局文件 layout/child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/child_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

具體實(shí)現(xiàn)代碼如下:
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
ExpandableListView expandview;
Map<String,List<String>> listMap = new HashMap<>();
String[] parentList = new String[]{"first","second","third"};
List<String> childList1 = new ArrayList<>();
List<String> childList2 = new ArrayList<>();
List<String> childList3 = new ArrayList<>();
private Button button;
BaseExpandableListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandview = (ExpandableListView) findViewById(R.id.expandlistview);
button = (Button) findViewById(R.id.updateData);

    //設(shè)置childList的數(shù)據(jù)
    childList1.add(parentList[0]+"first");
    childList1.add(parentList[1]+"first");
    childList1.add(parentList[2]+"first");
    childList2.add(parentList[0]+"first");
    childList2.add(parentList[1]+"first");
    childList2.add(parentList[2]+"first");
    childList3.add(parentList[0]+"first");
    childList3.add(parentList[1]+"first");
    childList3.add(parentList[2]+"first");

    listMap.put(parentList[0],childList1);
    listMap.put(parentList[1],childList2);
    listMap.put(parentList[2],childList3);

    //設(shè)置適配器
    adapter = new MyExpandableListAdapter(this);
    expandview.setAdapter(adapter);

// 這里在每個(gè)子項(xiàng)被點(diǎn)擊了之后會(huì)顯示是哪個(gè)子項(xiàng)被點(diǎn)擊了
// 特別注意
//(1)在使用這個(gè)方法的時(shí)候需要將自定義的adapter中的isChildSelectable方法的返回值設(shè)置為true卫玖,
// 否則子項(xiàng)的點(diǎn)擊不生效,但子項(xiàng)布局中設(shè)置的控件的監(jiān)聽器依然可以生效踊淳。
// // 子項(xiàng)是否可選中假瞬,如果需要設(shè)置子項(xiàng)的點(diǎn)擊事件,需要返回true
// @Override
// public boolean isChildSelectable(int i, int i1) {
// return true;
// }
//(2)如果在子項(xiàng)中對(duì)某個(gè)控件設(shè)置了監(jiān)聽器迂尝,這個(gè)控件要注意不能鋪滿整個(gè)子項(xiàng)笨触,
// 所以在設(shè)置高度和寬度時(shí)要特別注意,否則設(shè)置了子項(xiàng)的監(jiān)聽器也是沒有用的

    expandview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view,
                                    int parentPos, int childPos, long l) {
            Toast.makeText(MainActivity.this,
                    listMap.get(parentList[parentPos]).get(childPos), Toast.LENGTH_SHORT).show();
            return true;
        }
    });

// 然后設(shè)置ExpandableListView長(zhǎng)按item的監(jiān)聽器:
expandview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
String content = "";
if ((int) view.getTag(R.layout.child_item) == -1) {
content = "父類第" + view.getTag(R.layout.parent_item) + "項(xiàng)" + "被長(zhǎng)按了";
} else {
content = "父類第" + view.getTag(R.layout.parent_item) + "項(xiàng)" + "中的"
+ "子類第" + view.getTag(R.layout.child_item) + "項(xiàng)" + "被長(zhǎng)按了";
}
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
return true;
}
});

    //設(shè)置listview的展開和伸縮
    expandview.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(MainActivity.this, "第" + groupPosition + "個(gè)列表伸展了", Toast.LENGTH_SHORT).show();
        }
    });

    expandview.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(MainActivity.this, "第" + groupPosition + "個(gè)列表收縮了", Toast.LENGTH_SHORT).show();
        }
    });

// 如果需要進(jìn)入的時(shí)候列表就展開雹舀,然后不再收起芦劣,可以這樣設(shè)置:
// 《1》在setAdapter之后遍歷每一個(gè)列表使它們展開
// 《2》然后設(shè)置父類的監(jiān)聽器直接返回true即可,不可以設(shè)置父類的監(jiān)聽器為null说榆,
// 那樣起不到屏蔽原先系統(tǒng)設(shè)置的監(jiān)聽器的效果

    for (int i = 0; i < parentList.length; i++){
        if (!expandview.isGroupExpanded(i)) {
            expandview.expandGroup(i);
        }
    }
    expandview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {

// 用于判斷列表是否展開的方法
// if (expandview.isGroupExpanded(i)) {//列表已展開虚吟,返回true;列表未展開签财,返回false
// expandview.collapseGroup(i);
// } else {
//// 如果把這個(gè)參數(shù)設(shè)置為true串慰,列表展開的時(shí)候會(huì)有動(dòng)畫效果,該方法需在API大于等于14的時(shí)候才可以用
// expandview.expandGroup(i, true);
// }
return true;
}
});

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            updateData();
            Toast.makeText(MainActivity.this, "數(shù)據(jù)已更新", Toast.LENGTH_SHORT).show();
        }
    });
}

private void updateData() {
    childList1.clear();
    childList1.add(parentList[0] + "-new-" + "first");
    childList1.add(parentList[0] + "-new-" + "second");
    childList1.add(parentList[0] + "-new-" + "third");

    childList2.clear();
    childList2.add(parentList[1] + "-new-" + "first");
    childList2.add(parentList[1] + "-new-" + "second");
    childList2.add(parentList[1] + "-new-" + "third");


    childList3.clear();
    childList3.add(parentList[2] + "-new-" + "first");
    childList3.add(parentList[2] + "-new-" + "second");
    childList3.add(parentList[2] + "-new-" + "third");

    //刷新適配器
    adapter.notifyDataSetChanged();

}

private class MyExpandableListAdapter extends BaseExpandableListAdapter {
    Context mcontext;
    LayoutInflater inflater;
    public MyExpandableListAdapter(Context context) {
        mcontext = context;
        inflater = LayoutInflater.from(context);
    }

    //獲取父項(xiàng)的數(shù)據(jù)
    @Override
    public int getGroupCount() {
        return listMap.size();
    }
     //獲取子項(xiàng)的數(shù)據(jù)
    @Override
    public int getChildrenCount(int groupPosition) {
        return  listMap.get(parentList[groupPosition]).size();
    }

    //獲取某個(gè)父項(xiàng)
    @Override
    public Object getGroup(int groupPosition) {
        return listMap.get(parentList[groupPosition]);
    }

    //獲取某個(gè)父項(xiàng)的某個(gè)子項(xiàng)
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return listMap.get(parentList[groupPosition]).get(childPosition);
    }

    //獲取某個(gè)父項(xiàng)的id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //獲取某個(gè)子項(xiàng)的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //  按函數(shù)的名字來理解應(yīng)該是是否具有穩(wěn)定的id唱蒸,這個(gè)方法目前一直都是返回false邦鲫,沒有去改動(dòng)過
    @Override
    public boolean hasStableIds() {
        return false;
    }

// 當(dāng)expandlistview的getGroupView或者getChildView中包含checkbox時(shí),前者點(diǎn)擊不可用。
// 解決辦法 在etGroupView或者getChildView的checkbox中添加
// android:clickable="true"
// android:focusable="false"
// android:focusableInTouchMode="false"
// 獲得父項(xiàng)顯示的view
// ExpandableListView 的 數(shù)據(jù)適配器 ExpandableListAdapter 中的
// getGroupView 函數(shù)中所引入的自定義一級(jí)目錄xml 布局文件不能帶有 button庆捺,
// 否則會(huì)導(dǎo)致展開失效古今,ImageButton沒嘗試過,不過可能也是不行的滔以。

// 這里用到了view的setTag方法捉腥,一共設(shè)置了兩個(gè)Tag,
// 標(biāo)簽雖然在設(shè)置的時(shí)候提示說只要int類型即可你画,但一開始使用0和1來做tag的時(shí)候抵碟,
// 顯示沒有報(bào)錯(cuò),但編譯運(yùn)行就報(bào)錯(cuò)了坏匪,要求是資源文件的id才行拟逮,
// 因此換成了R.layout.parent_item和R.layout.child_item。
// 如果是父項(xiàng)适滓,就設(shè)置R.layout.parent_item為第幾個(gè)父項(xiàng)敦迄,
// 設(shè)置R.layout.child_item為-1。如果是子項(xiàng)粒竖,就設(shè)置R.layout.parent_item屬于第幾個(gè)父項(xiàng)
// 設(shè)置R.layout.child_item為該父項(xiàng)的第幾個(gè)子項(xiàng)颅崩,這樣就可以區(qū)分被長(zhǎng)按的是父項(xiàng)還是子項(xiàng)

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.parent_item, null);
        }
        convertView.setTag(R.layout.parent_item, groupPosition);
        convertView.setTag(R.layout.child_item, -1);

        TextView text = (TextView) convertView.findViewById(R.id.parent_title);
        text.setText(parentList[groupPosition]);
        return convertView;
    }
    //  獲得子項(xiàng)顯示的view
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null){
            convertView = inflater.inflate(R.layout.child_item, null);
        }

        convertView.setTag(R.layout.parent_item, groupPosition);
        convertView.setTag(R.layout.child_item, childPosition);

        TextView text = (TextView) convertView.findViewById(R.id.child_title);
        text.setText(listMap.get(parentList[groupPosition]).get(childPosition));

        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mcontext, "點(diǎn)到了內(nèi)置的textview", Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    //  子項(xiàng)是否可選中几于,如果需要設(shè)置子項(xiàng)的點(diǎn)擊事件蕊苗,需要返回true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市沿彭,隨后出現(xiàn)的幾起案子朽砰,更是在濱河造成了極大的恐慌,老刑警劉巖喉刘,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瞧柔,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡睦裳,警方通過查閱死者的電腦和手機(jī)造锅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來廉邑,“玉大人哥蔚,你說我怎么就攤上這事≈朊桑” “怎么了糙箍?”我有些...
    開封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)牵祟。 經(jīng)常有香客問我深夯,道長(zhǎng),這世上最難降的妖魔是什么诺苹? 我笑而不...
    開封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任咕晋,我火速辦了婚禮雹拄,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘捡需。我一直安慰自己办桨,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開白布站辉。 她就那樣靜靜地躺著呢撞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪饰剥。 梳的紋絲不亂的頭發(fā)上殊霞,一...
    開封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音汰蓉,去河邊找鬼绷蹲。 笑死,一個(gè)胖子當(dāng)著我的面吹牛顾孽,可吹牛的內(nèi)容都是我干的祝钢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼若厚,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼拦英!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起测秸,我...
    開封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤疤估,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后霎冯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體铃拇,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年沈撞,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了慷荔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出框沟,到底是詐尸還是另有隱情,我是刑警寧澤吧碾,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站墓卦,受9級(jí)特大地震影響倦春,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一睁本、第九天 我趴在偏房一處隱蔽的房頂上張望尿庐。 院中可真熱鬧,春花似錦呢堰、人聲如沸抄瑟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽皮假。三九已至,卻和暖如春骂维,著一層夾襖步出監(jiān)牢的瞬間惹资,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來泰國打工航闺, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留褪测,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓潦刃,卻偏偏與公主長(zhǎng)得像侮措,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子乖杠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容