標籤:檔案 etc clist 綁定 hold down stat delete for
前言眼下回到了工作崗位,第一件事情就是ExpandListView的最佳化。這裡簡單的用一個Demo介紹一下ExpandableListView的使用。
簡介一下Demo實現的功能,主要是繼承BaseExpandableListAdapter來自己定義adapter呈現ExpandableListView資料:
- 每一個child item有一個TextView和一個ImageView刪除標識。
- 當點擊一個child item,彈出Toast提示。
- child item能夠通過點擊刪除表徵圖來刪除。
- 每次展開特定group時。其它group自己主動收縮。
Demo效果展示:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3p5XzE5ODg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >
Android實現建立一個新的Android項目,我將其命名為expandtutorial。
XML布局檔案expandtutorial項目總共須要三個xml布局檔案。各自是activity_main.xml。 child_item.xml。 group_item.xml。
activity_main.xml
這個是用來布局ExpandableListView的,內容例如以下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > <ExpandableListView android:id="@+id/expandable_list_view_id" android:layout_width="match_parent" android:layout_height="match_parent" > </ExpandableListView></RelativeLayout>
group_item.xml
這是用來定義父列表的樣式,由一個TextView和一個ImageView構成
<?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="50dp" android:background="#ffffff" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:id="@+id/group_text" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="16dp" android:textColor="#808080" android:textSize="16sp" android:gravity="center_vertical" android:layout_alignParentLeft="true" android:text="@string/app_name"/> <ImageView android:id="@+id/group_indicator" android:src="@drawable/ic_guide_listview_down" android:contentDescription="@null" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginRight="16sp" android:layout_alignParentRight="true"/> </RelativeLayout>
child_item.xml這是用來定義孩子列表的樣式
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/child_item_id" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <TextView android:id="@+id/child_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:paddingLeft="25dp" /> <ImageView android:id="@+id/child_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:contentDescription="@null" android:src="@drawable/delete" /></RelativeLayout>
自己定義Adapter自己定義adapter是繼承自BaseExpandableListAdapter。有幾個須要注意的地方:
- 使用靜態內部類標識父item和子item的weidget,由於這些控制項的id是同樣的,這樣能夠節省掉每次findViewById的時間。
- 重寫(override)isChildSelectable方法,返回true,表示子item是能夠點擊的。
package com.example.expandtutorial;import java.util.ArrayList;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.graphics.Typeface;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ImageView;import android.widget.TextView;public class CustomerAdapter extends BaseExpandableListAdapter{private ArrayList<ParentObj> datas;private Context context;public CustomerAdapter(ArrayList<ParentObj> datas, Context context) {super();this.datas = datas;this.context = context;}@Overridepublic int getGroupCount() {return datas.size();}@Overridepublic int getChildrenCount(int groupPosition) {return datas.get(groupPosition).getChilds().size();}@Overridepublic Object getGroup(int groupPosition) {return datas.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {return datas.get(groupPosition).getChilds().get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Override// 是否有穩定的id。跟重新整理順序有關public boolean hasStableIds() {return false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {ParentViewHolder pviewHolder;if (convertView == null) {convertView = LayoutInflater.from(context).inflate(R.layout.group_item, parent, false);pviewHolder = new ParentViewHolder();pviewHolder.pTextView = (TextView) convertView.findViewById(R.id.group_text);pviewHolder.pImageView = (ImageView) convertView.findViewById(R.id.group_indicator);convertView.setTag(pviewHolder);}pviewHolder = (ParentViewHolder)convertView.getTag();pviewHolder.pTextView.setTypeface(null, Typeface.BOLD);pviewHolder.pTextView.setText(datas.get(groupPosition).getpName());if (isExpanded) {pviewHolder.pImageView.setImageResource(R.drawable.ic_guide_listview_up);} else {pviewHolder.pImageView.setImageResource(R.drawable.ic_guide_listview_down);}return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,ViewGroup parent) {ChildViewHolder cViewHolder;if (convertView == null) {convertView = LayoutInflater.from(context).inflate(R.layout.child_item, parent, false);cViewHolder = new ChildViewHolder();cViewHolder.cTextView = (TextView)convertView.findViewById(R.id.child_item);cViewHolder.cImageView = (ImageView) convertView.findViewById(R.id.child_delete);convertView.setTag(cViewHolder);}cViewHolder = (ChildViewHolder)convertView.getTag();cViewHolder.cTextView.setText(datas.get(groupPosition).getChilds().get(childPosition).getcName());final int gPosition = groupPosition;final int cPosition = childPosition;cViewHolder.cImageView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setMessage("Do you want to remove?");builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {datas.get(gPosition).getChilds().remove(cPosition);notifyDataSetChanged();}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.cancel();}});AlertDialog alertDialog = builder.create();alertDialog.show();}});return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}static class ParentViewHolder {TextView pTextView;ImageView pImageView;}static class ChildViewHolder {TextView cTextView;ImageView cImageView;}}
Activity我們的主Activity,用來呈現UI效果,這裡主要有兩個地方須要注意:
- ExpandableListView須要和自己定義adapter進行綁定,通過setAdapter方法。
- 預設ExpandableListView帶了一個指示箭頭。我們自己定義布局是不須要這個指示箭頭的。能夠使用expandableListView.setGroupIndicator(null);方法將其去掉。
- 設定setOnGroupExpandListener,當展開一個特定的group時,關閉其它group。
- 設定setOnChildClickListener,監聽孩子點擊事件。
package com.example.expandtutorial;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;import android.widget.ExpandableListView.OnGroupExpandListener;import android.widget.Toast;public class MainActivity extends Activity {private ExpandableListView expandableListView;private CustomerAdapter customerAdapter;private ArrayList<ParentObj> listData = new ArrayList<ParentObj>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);createGroupData();createChildData();expandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view_id);customerAdapter = new CustomerAdapter(listData, this);expandableListView.setAdapter(customerAdapter);expandableListView.setGroupIndicator(null);expandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {@Overridepublic void onGroupExpand(int groupPosition) {for (int i = 0; i < customerAdapter.getGroupCount(); i ++) {if (i != groupPosition) {expandableListView.collapseGroup(i);}}}});expandableListView.setOnChildClickListener(new OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {Toast.makeText(MainActivity.this, listData.get(groupPosition).getChilds().get(childPosition).getcName(), Toast.LENGTH_LONG).show();return true;}});}private void createGroupData() {ParentObj p1 = new ParentObj();p1.setpName("HP");listData.add(p1);ParentObj p2 = new ParentObj();p2.setpName("DELL");listData.add(p2);ParentObj p3 = new ParentObj();p3.setpName("Lenovo");listData.add(p3);ParentObj p4 = new ParentObj();p4.setpName("Sony");listData.add(p4);ParentObj p5 = new ParentObj();p5.setpName("HCL");listData.add(p5);ParentObj p6 = new ParentObj();p6.setpName("Samsung");listData.add(p6);}private void createChildData() {// preparing laptops collection(child)String[] hpModels = { "HP Pavilion G6-2014TX", "ProBook HP 4540", "HP Envy 4-1025TX" };String[] hclModels = { "HCL S2101", "HCL L2102", "HCL V2002" };String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series", "ThinkPad X Series", "Ideapad Z Series" };String[] sonyModels = { "VAIO E Series", "VAIO Z Series", "VAIO S Series", "VAIO YB Series" };String[] dellModels = { "Inspiron", "Vostro", "XPS" };String[] samsungModels = { "NP Series", "Series 5", "SF Series" };for (ParentObj p : listData) {if (p.getpName().equals("HP")) {ArrayList<ChildObj> clists = new ArrayList<ChildObj>();for (int i = 0; i < hpModels.length; i ++) {ChildObj cObj = new ChildObj();cObj.setcName(hpModels[i]);clists.add(cObj);}p.setChilds(clists);} else if (p.getpName().equals("DELL")) {ArrayList<ChildObj> clists = new ArrayList<ChildObj>();for (int i = 0; i < dellModels.length; i ++) {ChildObj cObj = new ChildObj();cObj.setcName(dellModels[i]);clists.add(cObj);}p.setChilds(clists);} else if (p.getpName().equals("Lenovo")) {ArrayList<ChildObj> clists = new ArrayList<ChildObj>();for (int i = 0; i < lenovoModels.length; i ++) {ChildObj cObj = new ChildObj();cObj.setcName(lenovoModels[i]);clists.add(cObj);}p.setChilds(clists);} else if (p.getpName().equals("Sony")) {ArrayList<ChildObj> clists = new ArrayList<ChildObj>();for (int i = 0; i < sonyModels.length; i ++) {ChildObj cObj = new ChildObj();cObj.setcName(sonyModels[i]);clists.add(cObj);}p.setChilds(clists);} else if (p.getpName().equals("HCL")) {ArrayList<ChildObj> clists = new ArrayList<ChildObj>();for (int i = 0; i < hclModels.length; i ++) {ChildObj cObj = new ChildObj();cObj.setcName(hclModels[i]);clists.add(cObj);}p.setChilds(clists);} else {ArrayList<ChildObj> clists = new ArrayList<ChildObj>();for (int i = 0; i < samsungModels.length; i ++) {ChildObj cObj = new ChildObj();cObj.setcName(samsungModels[i]);clists.add(cObj);}p.setChilds(clists);}}}}class ParentObj {private String pName;private ArrayList<ChildObj> childs = new ArrayList<ChildObj>();public String getpName() {return pName;}public void setpName(String pName) {this.pName = pName;}public ArrayList<ChildObj> getChilds() {return childs;}public void setChilds(ArrayList<ChildObj> childs) {this.childs.clear();this.childs.addAll(childs);}}class ChildObj {private String cName;public String getcName() {return cName;}public void setcName(String cName) {this.cName = cName;}}
原始碼我將原始碼上傳到CSDN,大家能夠免費下載,歡迎跟帖討論。原始碼地址:http://download.csdn.net/detail/zinss26914/7532205
Android:ExpandableListView使用