ExpandableListView是android中能夠實現下拉list的一個控件,是一個垂直滾動的心事兩個級別列表項手風琴試圖,列表項是來自ExpandableListViewaAdapter,組能夠單獨展開。java
重要方法:android
expandGroup (int groupPos) ;//在分組列表視圖中 展開一組, setSelectedGroup (int groupPosition) ;//設置選擇指定的組。 setSelectedChild (int groupPosition, int childPosition, boolean shouldExpandGroup);//設置選擇指定的子項。 getPackedPositionGroup (long packedPosition);//返回所選擇的組 getPackedPositionForChild (int groupPosition, int childPosition) ;//返回所選擇的子項 getPackedPositionType (long packedPosition);//返回所選擇項的類型(Child,Group) isGroupExpanded (int groupPosition);//判斷此組是否展開
expandableListView.setDivider();這個是設定每一個Group之間的分割線。web
expandableListView.setGroupIndicator();這個是設定每一個Group以前的那個圖標。
app
expandableListView.collapseGroup(int group); 將第group組收起ide
ExpandableListAdapter佈局
一個接口,將基礎數據連接到一個ExpandableListView。 此接口的實施將提供訪問Child的數據(由組分類),並實例化的Child和Group。this
1.重要方法spa
getChildId (int groupPosition, int childPosition) 獲取與在給定組給予孩子相關的數據。code
getChildrenCount (int groupPosition) 返回在指定Group的Child數目。orm
案例:
首先定義個一個佈局文件expandablelistview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ExpandableListView android:id ="@+id/expandableListView" android:layout_width ="fill_parent" android:layout_height ="wrap_content" > </ExpandableListView> </LinearLayout>
package com.test; import java.util.ArrayList; import java.util.List; import javax.security.auth.PrivateCredentialPermission; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; public class ExpandableListViewDemo extends Activity { /** Called when the activity is first created. */ //定義兩個List用來控制Group和Child中的String; private List<String> groupArray;//組列表 private List<List<String>> childArray;//子列表 private ExpandableListView expandableListView_one; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_NO_TITLE); //設置爲無標題 setContentView(R.layout.expandablelistview); expandableListView_one =(ExpandableListView)findViewById(R.id.expandableListView); groupArray =new ArrayList<String>(); childArray = new ArrayList<List<String>>(); /*-第一季-*/ initdate(); expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this)); /*-第二季-*/ // groupArray.add("移動開發"); // List<String> arrayList = new ArrayList<String>(); // arrayList.add("Android"); // arrayList.add("IOS"); // arrayList.add("Windows Phone"); // //組循環 // for(int index=0;index<groupArray.size();++index) // { // childArray.add(arrayList); // } // expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this)); } class ExpandableListViewaAdapter extends BaseExpandableListAdapter { Activity activity; public ExpandableListViewaAdapter(Activity a) { activity = a; } /*-----------------Child */ @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childArray.get(groupPosition).get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String string =childArray.get(groupPosition).get(childPosition); return getGenericView(string); } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return childArray.get(groupPosition).size(); } /* ----------------------------Group */ @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return getGroup(groupPosition); } @Override public int getGroupCount() { // TODO Auto-generated method stub return groupArray.size(); } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String string=groupArray.get(groupPosition); return getGenericView(string); } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } private TextView getGenericView(String string ) { AbsListView.LayoutParams layoutParams =new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); TextView textView =new TextView(activity); textView.setLayoutParams(layoutParams); textView.setGravity(Gravity.CENTER_VERTICAL |Gravity.LEFT); textView.setPadding(40, 0, 0, 0); textView.setText(string); return textView; } } private void initdate() { addInfo("語言", new String[]{"Oracle","Java","Linux","Jquery"}); addInfo("男人的需求", new String[]{"金錢","事業","權力","女人","房子","車","球"}); } private void addInfo(String group,String []child) { groupArray.add(group); List<String> childItem =new ArrayList<String>(); for(int index=0;index<child.length;index++) { childItem.add(child[index]); } childArray.add(childItem); } }
運行效果:
註釋修改以下代碼:
/*-第一季-*/ // initdate(); // expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this)); /*-第二季-*/ groupArray.add("移動開發"); List<String> arrayList = new ArrayList<String>(); arrayList.add("Android"); arrayList.add("IOS"); arrayList.add("Windows Phone"); //組循環 for(int index=0;index<groupArray.size();++index) { childArray.add(arrayList); } expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this));
運行效果:
★★★★★★★★★★★★★★★★★★★★
案例二:
1.定義一個主界面expandablelistview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ExpandableListView android:id ="@+id/expandableListView" android:layout_width ="fill_parent" android:layout_height ="wrap_content" > </ExpandableListView> </LinearLayout>
2.在res/drawable目錄下建立樣式文件expandablelistview_groups.xml該界面是組界面:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textGroup" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="40px" android:paddingTop="6px" android:paddingBottom="6px" android:textSize="15sp" android:text="No data" > </TextView> </LinearLayout>
3.在res/drawable目錄下建立樣式文件expandablelistview_child.xml;是子控件,直接顯示列表內容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textChild" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="60px" android:paddingTop="10px" android:paddingBottom="10px" android:textSize="20sp" android:text="No Data" /> </LinearLayout>
定義java文件:ExpandableListViewDemo_two.java
package com.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.security.auth.PrivateCredentialPermission; import com.test.R; import com.test.ExpandableListViewDemo.ExpandableListViewaAdapter; import com.test.R.id; import com.test.R.layout; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.SimpleExpandableListAdapter; import android.widget.TextView; public class ExpandableListViewDemo_two extends Activity { /** Called when the activity is first created. */ private ExpandableListView expandableListView_one; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.expandablelistview); expandableListView_one =(ExpandableListView)findViewById(R.id.expandableListView); //建立二個一級條目標題 Map<String, String> title_1 = new HashMap<String, String>(); Map<String, String> title_2 = new HashMap<String, String>(); title_1.put("group", "移動開發"); title_2.put("group", "男人的需求"); //建立一級條目容器 List<Map<String, String>> gruops = new ArrayList<Map<String,String>>(); gruops.add(title_1); gruops.add(title_2); //建立二級條目內容 //內容一 Map<String, String> content_1 = new HashMap<String, String>(); Map<String, String> content_2 = new HashMap<String, String>(); content_1.put("child", "ANDROID"); content_2.put("child", "IOS"); List<Map<String, String>> childs_1 = new ArrayList<Map<String,String>>(); childs_1.add(content_1); childs_1.add(content_2); //內容二 Map<String, String> content_3 = new HashMap<String, String>(); Map<String, String> content_4 = new HashMap<String, String>(); Map<String, String> content_5 = new HashMap<String, String>(); content_3.put("child", "金錢"); content_4.put("child", "權力"); content_5.put("child", "女人"); List<Map<String, String>> childs_2 = new ArrayList<Map<String,String>>(); childs_2.add(content_3); childs_2.add(content_4); childs_2.add(content_5); //存放兩個內容, 以便顯示在列表中 List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>(); childs.add(childs_1); childs.add(childs_2); //建立ExpandableList的Adapter容器 /** * 使用SimpleExpandableListAdapter顯示ExpandableListView * 參數1.上下文對象Context * 參數2.一級條目目錄集合 * 參數3.一級條目對應的佈局文件 (expandablelistview_groups.xml文件 * 參數4.fromto,就是map中的key,指定要顯示的對象 * 參數5.與參數4對應,指定要顯示在groups中的id * 參數6.二級條目目錄集合 * 參數7.二級條目對應的佈局文件 * 參數9.與參數8對應,指定要顯示在childs中的id / SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, gruops, R.drawable.expandablelistview_groups, new String[]{"group"}, new int[]{R.id.textGroup}, childs, R.drawable.expandablelistview_child, new String[]{"child"}, new int[]{R.id.textChild} ); //加入列表 expandableListView_one.setAdapter(adapter); expandableListView_one.setOnChildClickListener(listener); } private OnChildClickListener listener =new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub toast("點擊了"); return false; } }; private void toast(String str) { Toast.makeText(this, str, Toast.LENGTH_LONG).show(); } }
上面的樣式也能夠使用系統的自帶的樣式以下:
android.R.layout.simple_expandable_list_item_1,//層顯示樣式 ,系統自定義
android.R.layout.simple_expandable_list_item_2,
運行效果:
案例三:若是group中有個ImageVIew將會是什麼狀況呢?
在SimpleExpandableListAdapter中有以下方法:
private void bindView(View view, Map<String, ?> data, String[] from, int[] to) { int len = to.length; for (int i = 0; i < len; i++) { TextView v = (TextView)view.findViewById(to[i]); if (v != null) { v.setText((String)data.get(from[i])); } } }
從上面的方法中能夠看出 SimpleExpandableListAdapter把因此的View都當成TextView來處理了,而不像SimpleAdapter能夠自動判斷View的類型,自動綁定,解決版本就是重寫bingview回調一下試試:
public class MyExpandableListAdapter extends BaseExpandableListAdapter{ private void bindView(View view, Map<String, ?> data, String[] from, int[] to) { int len = to.length; boolean isBound = false; for (int i = 0; i < len; i++) { final View v = view.findViewById(to[i]); if (v!=null) { final Object _data = data.get(from[i]); String text = _data == null ? "" : data.toString(); if (text == null) { text = ""; } if (mViewBinder != null) {//若是Binder不爲空,使用Binder進行處理 isBound = mViewBinder.setViewValue(v, data.get(from[i]), text); } if (!isBound) {//若是Binder跳過,使用原來的方法進行處理 TextView _v = (TextView)v; _v.setText((String)data.get(from[i])); } } } } }