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è)箭頭可以這樣寫(xiě)
 android:groupIndicator="@drawable/picture"
    android:groupIndicator="@null"
    childIndicator
 用于設(shè)置子項(xiàng)前顯示的圖標(biāo),不設(shè)置的話默認(rèn)是沒(méi)有圖標(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>

子類(lèi)的布局文件 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)聽(tīng)器依然可以生效。
// // 子項(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)聽(tīng)器,這個(gè)控件要注意不能鋪滿整個(gè)子項(xiàng),
// 所以在設(shè)置高度和寬度時(shí)要特別注意,否則設(shè)置了子項(xiàng)的監(jiān)聽(tīng)器也是沒(méi)有用的

    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)聽(tīng)器:
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 = "父類(lèi)第" + view.getTag(R.layout.parent_item) + "項(xiàng)" + "被長(zhǎng)按了";
} else {
content = "父類(lèi)第" + view.getTag(R.layout.parent_item) + "項(xiàng)" + "中的"
+ "子類(lèi)第" + view.getTag(R.layout.child_item) + "項(xiàng)" + "被長(zhǎng)按了";
}
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
return true;
}
});

    //設(shè)置listview的展開(kāi)和伸縮
    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í)候列表就展開(kāi),然后不再收起,可以這樣設(shè)置:
// 《1》在setAdapter之后遍歷每一個(gè)列表使它們展開(kāi)
// 《2》然后設(shè)置父類(lèi)的監(jiān)聽(tīng)器直接返回true即可,不可以設(shè)置父類(lèi)的監(jiān)聽(tīng)器為null,
// 那樣起不到屏蔽原先系統(tǒng)設(shè)置的監(jiān)聽(tīng)器的效果

    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) {

// 用于判斷列表是否展開(kāi)的方法
// if (expandview.isGroupExpanded(i)) {//列表已展開(kāi),返回true;列表未展開(kāi),返回false
// expandview.collapseGroup(i);
// } else {
//// 如果把這個(gè)參數(shù)設(shè)置為true,列表展開(kāi)的時(shí)候會(huì)有動(dòng)畫(huà)效果,該方法需在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ù)的名字來(lái)理解應(yīng)該是是否具有穩(wěn)定的id,這個(gè)方法目前一直都是返回false,沒(méi)有去改動(dòng)過(guò)
    @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)致展開(kāi)失效,ImageButton沒(méi)嘗試過(guò),不過(guò)可能也是不行的。

// 這里用到了view的setTag方法,一共設(shè)置了兩個(gè)Tag,
// 標(biāo)簽雖然在設(shè)置的時(shí)候提示說(shuō)只要int類(lèi)型即可,但一開(kāi)始使用0和1來(lái)做tag的時(shí)候,
// 顯示沒(méi)有報(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)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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