您好,登錄后才能下訂單哦!
今天休息時,我在讀一本書,包著書皮的緣由,項目經(jīng)理不知我在讀什么,遂問,你讀什么書,我隨口道“biancheng”。項目經(jīng)理聽罷,滿面笑容,旋即對他一側(cè)的一個人(也是我的同事)講,你看人家知道在休息時間補充能量,你只知道玩手機,差距就是這樣一點一點落下來的!我當時偷笑,委實只能這樣了,我是在讀“biancheng”,只不過該書的作者是沈從文。
言歸正傳,項目中需要用到ExpandableListView,自己寫了一個Demo,將它記錄于此。
先看看主要的XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="24sp" android:text="@string/the_two_provinces_linkage" /> <!-- 兩個自定義的expandablelistview控件 --> <com.example.expandablelistviewtest.ScrollExpandableListView android:id="@+id/provinceList" android:layout_width="match_parent" android:layout_height="wrap_content" android:childDivider="@drawable/child_bg" /> <com.example.expandablelistviewtest.ScrollExpandableListView android:id="@+id/cityList" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" android:childDivider="@drawable/child_bg" /> </LinearLayout> </ScrollView> </LinearLayout>
我在布局中使用了“ScrollView”,防止一頁內(nèi)容過多,手機屏幕顯示不下,那么,問題就來了:ScrollView會和ExpandableListView造成沖突。所以我又自定義了一個叫做“ScrollExpandableListView”的控件,代碼如下:
package com.example.expandablelistviewtest; import android.content.Context; import android.util.AttributeSet; import android.widget.ExpandableListView; public class ScrollExpandableListView extends ExpandableListView { public ScrollExpandableListView(Context context, AttributeSet attrs) { super(context, attrs); } /* 在ScrollView中嵌套使用ExpandableListView,ExpandableListView只會顯示一行多一點。 兩者進行嵌套,即會發(fā)生沖突.需要重寫OnMesure,對ListView或者GridView同樣適用*/ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
同樣做了一些有關(guān)省市的模擬數(shù)據(jù):
package com.example.expandablelistviewtest; import java.util.ArrayList; import java.util.HashMap; public class ProvinceData { private ArrayList<String> provinceGroupData;// 定義省級組數(shù)據(jù) private ArrayList<ArrayList<String>> provinceChildrenData;// 定義省級組中的子數(shù)據(jù) private ArrayList<String> cityGroupData;// 定義市級組數(shù)據(jù) private ArrayList<ArrayList<String>> cityChildrenData;// 定義市級組數(shù)據(jù)中的子數(shù)據(jù) private HashMap<String, ArrayList<String>> provinceToCityMap;// 創(chuàng)建省市的鍵值對 public ArrayList<String> getProvinceGroupData() { provinceGroupData = new ArrayList<String>(); provinceGroupData.add("省級名稱"); return provinceGroupData; } public ArrayList<ArrayList<String>> getProvinceChildrenData() { provinceChildrenData = new ArrayList<ArrayList<String>>(); ArrayList<String> provinceList = new ArrayList<String>(); provinceList.add("北京市"); provinceList.add("上海市"); provinceList.add("廣東省"); provinceList.add("浙江省"); provinceList.add("江蘇省"); provinceList.add("湖北省"); provinceList.add("山西省"); provinceList.add("河北省"); provinceChildrenData.add(provinceList); return provinceChildrenData; } public ArrayList<String> getCityGroupData() { cityGroupData = new ArrayList<String>(); cityGroupData.add("市級名稱"); return cityGroupData; } public ArrayList<ArrayList<String>> getcityChildrenData() { cityChildrenData = new ArrayList<ArrayList<String>>(); ArrayList<String> beijingList = new ArrayList<String>(); beijingList.add("海淀區(qū)"); beijingList.add("豐臺區(qū)"); beijingList.add("昌平區(qū)"); beijingList.add("密云縣"); cityChildrenData.add(beijingList); ArrayList<String> shanghaiList = new ArrayList<String>(); shanghaiList.add("嘉定區(qū)"); shanghaiList.add("浦東新區(qū)"); cityChildrenData.add(shanghaiList); ArrayList<String> guangdongList = new ArrayList<String>(); guangdongList.add("廣州市"); guangdongList.add("珠海市"); guangdongList.add("佛山市"); guangdongList.add("中山市"); cityChildrenData.add(guangdongList); ArrayList<String> zhejiangList = new ArrayList<String>(); zhejiangList.add("杭州市"); zhejiangList.add("寧波市"); zhejiangList.add("嘉興市"); cityChildrenData.add(zhejiangList); ArrayList<String> jiangsuList = new ArrayList<String>(); jiangsuList.add("南京市"); jiangsuList.add("徐州市"); jiangsuList.add("揚州市"); cityChildrenData.add(jiangsuList); ArrayList<String> hubeiList = new ArrayList<String>(); hubeiList.add("武漢市"); hubeiList.add("宜昌市"); hubeiList.add("荊門市"); hubeiList.add("黃岡市"); cityChildrenData.add(hubeiList); ArrayList<String> shanxiList = new ArrayList<String>(); shanxiList.add("太原市"); shanxiList.add("大同市"); shanxiList.add("陽泉市"); cityChildrenData.add(shanxiList); ArrayList<String> hebeiList = new ArrayList<String>(); hebeiList.add("石家莊市"); hebeiList.add("唐山市"); hebeiList.add("保定市"); cityChildrenData.add(hebeiList); return cityChildrenData; } public HashMap<String, ArrayList<String>> getProvinceAndCity() { ArrayList<ArrayList<String>> cityChildrenList = getcityChildrenData(); provinceToCityMap = new HashMap<String, ArrayList<String>>(); for (int i = 0; i < provinceChildrenData.get(0).size(); i++) { provinceToCityMap.put(provinceChildrenData.get(0).get(i), cityChildrenList.get(i)); } return provinceToCityMap; } }
接下來是主要的一個類:
package com.example.expandablelistviewtest; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; public class ExpandableListViewTest extends Activity { private ScrollExpandableListView proviceList; private ScrollExpandableListView cityList; private ArrayList<String> provinceGroupData;// 定義省級組數(shù)據(jù) private ArrayList<ArrayList<String>> provinceChildrenData;// 定義省級組中的子數(shù)據(jù) private ArrayList<String> cityGroupData;// 定義市級組數(shù)據(jù) private ArrayList<ArrayList<String>> cityChildrenData;// 定義市級組數(shù)據(jù)中的子數(shù)據(jù) private ProvinceData provinceData; // 定義一個兩個長度的數(shù)組,存放點中條目的組下標和子下標 private int[] provinceIndex = new int[2]; private int[] cityIndex = new int[2]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_expandable_list_view_test); provinceIndex[0] = -1; provinceIndex[1] = -1; provinceData = new ProvinceData(); provinceGroupData = provinceData.getProvinceGroupData(); provinceChildrenData = provinceData.getProvinceChildrenData(); proviceList = (ScrollExpandableListView) findViewById(R.id.provinceList); //去掉ExpandableListView默認的箭頭,我的手機貌似不用這一句也沒有默認的箭頭,誰能告訴我怎么回事…… proviceList.setGroupIndicator(null); final ScrollExpandableListAdapter provinceAdapter = new ScrollExpandableListAdapter( this, provinceGroupData, provinceChildrenData, provinceIndex); proviceList.setAdapter(provinceAdapter); cityList = (ScrollExpandableListView) findViewById(R.id.cityList); cityGroupData = provinceData.getCityGroupData(); proviceList.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // Expandablelistview 點擊子條目后收起列表 proviceList.collapseGroup(groupPosition); provinceIndex[0] = groupPosition; provinceIndex[1] = childPosition; cityIndex[0] = -1; cityIndex[1] = -1; String provinceName = provinceChildrenData.get(0).get(childPosition); cityList.setVisibility(View.VISIBLE); cityList.setGroupIndicator(null); cityChildrenData = new ArrayList<ArrayList<String>>(); ArrayList<String> cityName = provinceData.getProvinceAndCity().get( provinceName); cityChildrenData.add(cityName); ScrollExpandableListAdapter cityAdapter = new ScrollExpandableListAdapter( ExpandableListViewTest.this, cityGroupData, cityChildrenData,cityIndex); cityList.setAdapter(cityAdapter); cityList.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { cityIndex[0] = groupPosition; cityIndex[1] = childPosition; cityList.collapseGroup(groupPosition); return false; } }); return false; } }); } }
最重要的構(gòu)造器:
package com.example.expandablelistviewtest; import java.util.ArrayList; import android.content.Context; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; public class ScrollExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private ArrayList<String> groupList; private ArrayList<ArrayList<String>> childList; private LayoutInflater layoutInflater; // 定義一個兩個長度的數(shù)組,存放點中條目的組下標和子下標 private int[] index = new int[2]; private String groupContent; public ScrollExpandableListAdapter(Context context, ArrayList<String> groupList, ArrayList<ArrayList<String>> childList, int[] index) { this.context = context; this.groupList = groupList; this.childList = childList; this.index = index; layoutInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder cHolder = new ChildViewHolder(); if (convertView == null) { convertView = layoutInflater.inflate(R.layout.item_child, null); cHolder.childContent = (TextView) convertView .findViewById(R.id.childContent); cHolder.childCheck = (CheckBox) convertView .findViewById(R.id.check); convertView.setTag(cHolder); } else { cHolder = (ChildViewHolder) convertView.getTag(); } cHolder.childContent.setText(childList.get(groupPosition).get( childPosition)); if (index[0] == groupPosition && index[1] == childPosition) { cHolder.childCheck.setVisibility(View.VISIBLE); } else { cHolder.childCheck.setVisibility(View.INVISIBLE); } return convertView; } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public int getGroupCount() { return groupList.size(); } @Override public long getGroupId(int groupPosition) { return 0; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder gHolder = new GroupViewHolder(); if (convertView == null) { convertView = layoutInflater.inflate(R.layout.item_group, null); gHolder.groupName = (TextView) convertView .findViewById(R.id.groupName); gHolder.groupContent = (TextView) convertView .findViewById(R.id.groupContent); gHolder.groupPic = (ImageView) convertView .findViewById(R.id.groupPic); convertView.setTag(gHolder); } else { gHolder = (GroupViewHolder) convertView.getTag(); } //首次的話用戶沒有選擇省列表中的子項目,組內(nèi)容中是沒有數(shù)據(jù)的 if (index[0] != -1 && index[1] != -1) { groupContent = childList.get(index[0]).get(index[1]); } else { groupContent = ""; } gHolder.groupName.setText(groupList.get(groupPosition)); gHolder.groupPic.setBackgroundResource(R.drawable.arrow); if (!TextUtils.isEmpty(groupContent)) { gHolder.groupContent.setText(groupContent); } return convertView; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { index[0] = groupPosition; index[1] = childPosition; // 實現(xiàn)ChildView點擊事件,必須返回true return true; } private class GroupViewHolder { TextView groupName; TextView groupContent; ImageView groupPic; } private class ChildViewHolder { TextView childContent; CheckBox childCheck; } }
組Item與子Item的布局都是自定義的,它們的XML文件分別如下:
組XML:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ddd" android:gravity="center" > <RelativeLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="10dp" android:paddingRight="10dp" > <TextView android:id="@+id/groupName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="@string/group_name" android:textSize="20sp" /> <TextView android:id="@+id/groupContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/groupPic" android:textSize="20sp" /> <ImageView android:id="@id/groupPic" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/arrow" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:layout_below="@id/layout" android:background="#ccc" /> </RelativeLayout>
子XML:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:paddingLeft="10dp" android:paddingRight="10dp"> <TextView android:id="@+id/childContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/check" android:text="BBB" android:textSize="20sp" /> <CheckBox android:id="@id/check" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@drawable/checked" android:button="@null" android:visibility="gone" /> </RelativeLayout>
興許需要講一下設置ExpandableListView的子Item的分割線的問題,先前用“setChildDivider(null);這個方法來著,但是會報如下空指針錯誤:
我還不知道這句代碼應寫在何處,知道的朋友請告訴一下我。
我只好寫一個XML文件來代替了,如下:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape> <!-- 設置ExpandableListView的Child分割線為透明 --> <!-- <solid android:color="#0000" /> --> <!-- 設置ExpandableListView的分割線為紅色 --> <solid android:color="#f00" /> </shape> </item> </layer-list>
設置成透明即可實現(xiàn)沒有分割線的效果,為了直觀些,此處我改為了紅色。
事實上就這段Demo而言,完全可以做成一個ExpandableListView的,此處用了兩個,是我在項目開發(fā)中另有用途。
最后看一下效果圖吧:
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。