本節引言:
本節要講解的Adapter類控件是ExpandableListView,就是可摺疊的列表,它是ListView的子類, 在ListView的基礎上它把應用中的列表項分爲幾組,每組裏又可包含多個列表項。至於樣子, 類似於QQ聯繫人列表,他的用法與ListView非常相似,只是ExpandableListVivew顯示的列表項 需由ExpandableAdapter提供。 下面我們來學習這個控件的基本使用! 官方API:ExpandableListView
這裏需要說明一下:原生的ExpandableListView是不支持動畫效果的,如果想實現動畫效果建議使用GIT上的開源控件,參考文章:點擊打開鏈接
1.相關屬性
- android:childDivider:指定各組內子類表項之間的分隔條,圖片不會完全顯示, 分離子列表項的是一條直線
- android:childIndicator:顯示在子列表旁邊的Drawable對象,可以是一個圖像
- android:childIndicatorEnd:子列表項指示符的結束約束位置
- android:childIndicatorLeft:子列表項指示符的左邊約束位置
- android:childIndicatorRight:子列表項指示符的右邊約束位置
- android:childIndicatorStart:子列表項指示符的開始約束位置
- android:groupIndicator:顯示在組列表旁邊的Drawable對象,可以是一個圖像
- android:indicatorEnd:組列表項指示器的結束約束位置
- android:indicatorLeft:組列表項指示器的左邊約束位置
- android:indicatorRight:組列表項指示器的右邊約束位置
- android:indicatorStart:組列表項指示器的開始約束位置
2.實現ExpandableAdapter的三種方式
1. 擴展BaseExpandableListAdpter實現ExpandableAdapter。
2. 使用SimpleExpandableListAdpater將兩個List集合包裝成ExpandableAdapter
3. 使用simpleCursorTreeAdapter將Cursor中的數據包裝成SimpleCuroTreeAdapter 本節示例使用的是第一個,擴展BaseExpandableListAdpter,我們需要重寫該類中的相關方法, 下面我們通過一個代碼示例來體驗下!
3.代碼示例
我們來看下實現的效果圖:
下面我們就來實現上圖的這個效果:
核心是重寫BaseExpandableListAdpter,其實和之前寫的普通的BaseAdapter是類似的, 但是BaseExpandableListAdpter則分成了兩部分:組和子列表,具體看代碼你就知道了!
另外,有一點要注意的是,重寫isChildSelectable()方法需要返回true,不然不會觸發 子Item的點擊事件!下面我們來寫寫:
首先是組和子列表的佈局:
item_exlist_group.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="horizontal" android:padding="5dp"> <TextView android:id="@+id/tv_group_name" android:layout_width="match_parent" android:layout_height="56dp" android:gravity="center_vertical" android:paddingLeft="30dp" android:text="AP" android:textStyle="bold" android:textSize="20sp" /> </LinearLayout>
item_exlist_item.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="horizontal" android:padding="5dp" android:background="#6BBA79"> <ImageView android:id="@+id/img_icon" android:layout_width="48dp" android:layout_height="48dp" android:src="@mipmap/iv_lol_icon1" android:focusable="false"/> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="15dp" android:focusable="false" android:text="提莫" android:textSize="18sp" /> </LinearLayout>
然後是自定義的Adapter類:
MyBaseExpandableListAdapter.java:
/** * Created by Jay on 2015/9/25 0025. */ public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter { private ArrayList<Group> gData; private ArrayList<ArrayList<Item>> iData; private Context mContext; public MyBaseExpandableListAdapter(ArrayList<Group> gData,ArrayList<ArrayList<Item>> iData, Context mContext) { this.gData = gData; this.iData = iData; this.mContext = mContext; } @Override public int getGroupCount() { return gData.size(); } @Override public int getChildrenCount(int groupPosition) { return iData.get(groupPosition).size(); } @Override public Group getGroup(int groupPosition) { return gData.get(groupPosition); } @Override public Item getChild(int groupPosition, int childPosition) { return iData.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } //取得用於顯示給定分組的視圖. 這個方法僅返回分組的視圖對象 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewHolderGroup groupHolder; if(convertView == null){ convertView = LayoutInflater.from(mContext).inflate( R.layout.item_exlist_group, parent, false); groupHolder = new ViewHolderGroup(); groupHolder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name); convertView.setTag(groupHolder); }else{ groupHolder = (ViewHolderGroup) convertView.getTag(); } groupHolder.tv_group_name.setText(gData.get(groupPosition).getgName()); return convertView; } //取得顯示給定分組給定子位置的數據用的視圖 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ViewHolderItem itemHolder; if(convertView == null){ convertView = LayoutInflater.from(mContext).inflate( R.layout.item_exlist_item, parent, false); itemHolder = new ViewHolderItem(); itemHolder.img_icon = (ImageView) convertView.findViewById(R.id.img_icon); itemHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name); convertView.setTag(itemHolder); }else{ itemHolder = (ViewHolderItem) convertView.getTag(); } itemHolder.img_icon.setImageResource(iData.get(groupPosition).get(childPosition).getiId()); itemHolder.tv_name.setText(iData.get(groupPosition).get(childPosition).getiName()); return convertView; } //設置子列表是否可選中 @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } private static class ViewHolderGroup{ private TextView tv_group_name; } private static class ViewHolderItem{ private ImageView img_icon; private TextView tv_name; } }
PS:存儲子列表的數據不一定要用ArrayList<ArrayList>這種,根據自己的需求 定義~
最後是MainActivity的佈局以及Java代碼:
佈局文件:activity_main.xml:
<RelativeLayout 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" android:padding="5dp" tools:context=".MainActivity"> <ExpandableListView android:id="@+id/exlist_lol" android:layout_width="match_parent" android:layout_height="match_parent" android:childDivider="#E02D2F"/> </RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity { private ArrayList<Group> gData = null; private ArrayList<ArrayList<Item>> iData = null; private ArrayList<Item> lData = null; private Context mContext; private ExpandableListView exlist_lol; private MyBaseExpandableListAdapter myAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; exlist_lol = (ExpandableListView) findViewById(R.id.exlist_lol); //數據準備 gData = new ArrayList<Group>(); iData = new ArrayList<ArrayList<Item>>(); gData.add(new Group("AD")); gData.add(new Group("AP")); gData.add(new Group("TANK")); lData = new ArrayList<Item>(); //AD組 lData.add(new Item(R.mipmap.iv_lol_icon3,"劍聖")); lData.add(new Item(R.mipmap.iv_lol_icon4,"德萊文")); lData.add(new Item(R.mipmap.iv_lol_icon13,"男槍")); lData.add(new Item(R.mipmap.iv_lol_icon14,"韋魯斯")); iData.add(lData); //AP組 lData = new ArrayList<Item>(); lData.add(new Item(R.mipmap.iv_lol_icon1, "提莫")); lData.add(new Item(R.mipmap.iv_lol_icon7, "安妮")); lData.add(new Item(R.mipmap.iv_lol_icon8, "天使")); lData.add(new Item(R.mipmap.iv_lol_icon9, "澤拉斯")); lData.add(new Item(R.mipmap.iv_lol_icon11, "狐狸")); iData.add(lData); //TANK組 lData = new ArrayList<Item>(); lData.add(new Item(R.mipmap.iv_lol_icon2, "諾手")); lData.add(new Item(R.mipmap.iv_lol_icon5, "德邦")); lData.add(new Item(R.mipmap.iv_lol_icon6, "奧拉夫")); lData.add(new Item(R.mipmap.iv_lol_icon10, "龍女")); lData.add(new Item(R.mipmap.iv_lol_icon12, "狗熊")); iData.add(lData); myAdapter = new MyBaseExpandableListAdapter(gData,iData,mContext); exlist_lol.setAdapter(myAdapter); //爲列表設置點擊事件 exlist_lol.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(mContext, "你點擊了:" + iData.get(groupPosition).get(childPosition).getiName(), Toast.LENGTH_SHORT).show(); return true; } }); } }1. 設置ExpandableListView 默認是展開的:
先實例化exListView 然後
- exListView.setAdapter(exlvAdapter);
- //遍歷所有group,將所有項設置成默認展開
- intgroupCount = exListView.getCount();
- for (inti=0; i<groupCount; i++)
- {
- exListView.expandGroup(i);
- };
2. 去掉ExpandableListView 默認的箭頭
用到ExpandableListView時有個箭頭圖標系統自帶的在你自定義佈局也不能去掉只要設置一個屬性即可,如下:
settingLists.setGroupIndicator(null); ~~~~~~~~~~~~~~~~~此處就是設置自定義的箭頭圖標的。置空則沒有了。
也可以自定義(但是位置還是在那個地方不推薦)如下:
首先,自定義一個expandablelistviewselector.xml文件,具體內容如下: Java代碼
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_expanded="true" android:drawable="@drawable/expandablelistviewindicatordown" />
- <item android:drawable="@drawable/expandablelistviewindicator" />
- </selector>
加一句代碼如下:
- settingLists.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector));
- 或xml設置:
- android:groupIndicator="@drawable/groupIndicator_selector"
大功告成
1首先ExpandableListViewelistview;
elistview.setGroupIndicator(null);//將控件默認的左邊箭頭去掉,
2在自定義的繼承自BaseExpandableListAdapter的adapter中有一個方法
- /** * 父類view */ @Override
- ublic View getGroupView(intgroupPosition, booleanisExpanded, View convertView, ViewGroup parent)
- { Log.i("zhaoxiong","parent view");
- LinearLayoutparentLayout=(LinearLayout) View.inflate(context, R.layout.wowocoupons_parent_item, null);
- TextViewparentTextView=(TextView)parentLayout.findViewById(R.id.parentitem);
- parentTextView.setText(parentlist.get(groupPosition));
- ImageViewparentImageViw=(ImageView) parentLayout.findViewById(R.id.arrow);
- //判斷isExpanded就可以控制是按下還是關閉,同時更換圖片
- if(isExpanded){
- parentImageViw.setBackgroundResource(R.drawable.arrow_down);
- }else{
- parentImageViw.setBackgroundResource(R.drawable.arrow_up); }
- return parentLayout;
- }
expandablelistview響應onGroupClick監聽:設置expandablelistview.setOnGroupClickListener()
摺疊和展開事件,可以設置setOnGroupCollapseListener和setOnGroupExpandListener
ExpandableListView中包含多個group,想要展開一個group時,其他group都關閉:
- exList.setOnGroupExpandListener(new OnGroupExpandListener() {
- @Override
- public void onGroupExpand(int groupPosition) {
- for (int i = 0; i < getData().size(); i++) {
- if (groupPosition != i) {
- exList.collapseGroup(i);
- }
- }
- }
- });
- tt_list.setOnGroupClickListener(new OnGroupClickListener() {
- @Override
- public boolean onGroupClick(ExpandableListView parent, View v,
- int groupPosition, long id) {
- IsFlag=true;
- if(adapter.getGroupData().get(groupPosition).getList().size()==1){
- Bundle b=new Bundle();
- b.putInt("saveIndex", 0);
- // b.putString("mac", mac);
- // b.putString("deviceId", mDeviceId);
- b.putSerializable("datalist", adapter.getGroupData().get(groupPosition).getList());
- Intent i=new Intent(WappushBindingActivity.this,VideoPlayerActivity.class);
- i.putExtras(b);
- startActivity(i);
- }
- // int groupCount = tt_list.getCount();
- // for (int i=0; i<groupCount; i++){
- // if(i!=GroupPosition)
- // tt_list.collapseGroup(i);
- // };
- // Log.v("xulongheng*WappushBind*tt_list", "onGroupClick:"+previousX+"/"+previousY);
- return true; //默認爲false,設爲true時,點擊事件不會展開Group
- }
- });