Android BaseExpandableListAdapter 教程

转自:http://fonter.iteye.com/blog/684238


先上图再说,实现效果如下图,选项可多少可变化。

 

 

 

BaseExpandableListAdapter实现

 

Java代码   收藏代码
  1. import java.util.List;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.drawable.Drawable;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseExpandableListAdapter;  
  9. import android.widget.CheckBox;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12. import com.iwidsets.clear.manager.R;  
  13. import com.iwidsets.clear.manager.adapter.BrowserInfo;  
  14.   
  15. public class ClearExpandableListAdapter extends BaseExpandableListAdapter {  
  16.   
  17.     class ExpandableListHolder {  
  18.         ImageView appIcon;  
  19.         TextView appInfo;  
  20.         CheckBox appCheckBox;  
  21.     }  
  22.   
  23.     private Context context;  
  24.     private LayoutInflater mChildInflater;  
  25.     private LayoutInflater mGroupInflater;  
  26.     private List<GroupInfo> group;  
  27.   
  28.     public ClearExpandableListAdapter(Context c, List<GroupInfo> group) {  
  29.         this.context = c;  
  30.         mChildInflater = (LayoutInflater) context  
  31.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  32.         mGroupInflater = (LayoutInflater) context  
  33.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  34.         this.group = group;  
  35.     }  
  36.   
  37.     public Object getChild(int childPosition, int itemPosition) {  
  38.         return group.get(childPosition).getChild(itemPosition);  
  39.     }  
  40.   
  41.     public long getChildId(int childPosition, int itemPosition) {  
  42.   
  43.         return itemPosition;  
  44.     }  
  45.   
  46.     @Override  
  47.     public int getChildrenCount(int index) {  
  48.         return group.get(index).getChildSize();  
  49.     }  
  50.   
  51.     public Object getGroup(int index) {  
  52.         return group.get(index);  
  53.     }  
  54.   
  55.     public int getGroupCount() {  
  56.         return group.size();  
  57.     }  
  58.   
  59.     public long getGroupId(int index) {  
  60.         return index;  
  61.     }  
  62.   
  63.     public View getGroupView(int position, boolean flag, View view,  
  64.             ViewGroup parent) {  
  65.         ExpandableListHolder holder = null;  
  66.         if (view == null) {  
  67.             view = mGroupInflater.inflate(  
  68.                     R.layout.browser_expandable_list_item, null);  
  69.             holder = new ExpandableListHolder();  
  70.             holder.appIcon = (ImageView) view.findViewById(R.id.app_icon);  
  71.             view.setTag(holder);  
  72.             holder.appInfo = (TextView) view.findViewById(R.id.app_info);  
  73.             holder.appCheckBox = (CheckBox) view  
  74.                     .findViewById(R.id.app_checkbox);  
  75.         } else {  
  76.             holder = (ExpandableListHolder) view.getTag();  
  77.         }  
  78.         GroupInfo info = this.group.get(position);  
  79.         if (info != null) {  
  80.             holder.appInfo.setText(info.getBrowserInfo().getAppInfoId());  
  81.             Drawable draw = this.context.getResources().getDrawable(  
  82.                     info.getBrowserInfo().getImageId());  
  83.             holder.appIcon.setImageDrawable(draw);  
  84.             holder.appCheckBox.setChecked(info.getBrowserInfo().isChecked());  
  85.         }  
  86.         return view;  
  87.     }  
  88.   
  89.     public boolean hasStableIds() {  
  90.         return false;  
  91.     }  
  92.   
  93.     public boolean isChildSelectable(int arg0, int arg1) {  
  94.         return false;  
  95.     }  
  96.   
  97.     @Override  
  98.     public View getChildView(int groupPosition, int childPosition,  
  99.             boolean isLastChild, View convertView, ViewGroup parent) {  
  100.         ExpandableListHolder holder = null;  
  101.         if (convertView == null) {  
  102.             convertView = mChildInflater.inflate(  
  103.                     R.layout.browser_expandable_list_item, null);  
  104.             holder = new ExpandableListHolder();  
  105.             holder.appIcon = (ImageView) convertView  
  106.                     .findViewById(R.id.app_icon);  
  107.             convertView.setTag(holder);  
  108.             holder.appInfo = (TextView) convertView.findViewById(R.id.app_info);  
  109.             holder.appCheckBox = (CheckBox) convertView  
  110.                     .findViewById(R.id.app_checkbox);  
  111.         } else {  
  112.             holder = (ExpandableListHolder) convertView.getTag();  
  113.         }  
  114.         BrowserInfo info = this.group.get(groupPosition)  
  115.                 .getChild(childPosition);  
  116.         if (info != null) {  
  117.             holder.appInfo.setText(info.getAppInfoId());  
  118.             Drawable draw = this.context.getResources().getDrawable(  
  119.                     info.getImageId());  
  120.             holder.appIcon.setImageDrawable(draw);  
  121.             holder.appCheckBox.setChecked(info.isChecked());  
  122.         }  
  123.         return convertView;  
  124.     }  
  125.   
  126. }  

 

要想让child获得焦点,只在改

 

Java代码   收藏代码
  1. public boolean isChildSelectable(int arg0, int arg1) {  
  2.     return true;  
  3. }  

  

 

browser_expandable_list_item.xml 用于显示每一个ITEM的XML

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="wrap_content"  
  4.     android:orientation="horizontal" android:minHeight="40px"  
  5.     android:layout_gravity="center_vertical">  
  6.     <CheckBox android:id="@+id/app_checkbox" android:focusable="false"  
  7.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  8.         android:layout_marginLeft="35px" android:checked="true"/>  
  9.     <ImageView android:id="@+id/app_icon" android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content" android:layout_gravity="center_vertical" />  
  11.   
  12.     <TextView android:id="@+id/app_info" android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content" android:textColor="?android:attr/textColorPrimary"  
  14.         android:paddingLeft="3px" android:layout_gravity="center_vertical" />  
  15. </LinearLayout>  

 

 

browserlayout.xml 用于显示整个界面

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4.     android:orientation="vertical">  
  5.     <ExpandableListView android:id="@+id/appList"  
  6.         android:layout_width="fill_parent" android:layout_height="0dip"  
  7.         android:layout_weight="1" />  
  8.     <LinearLayout android:layout_height="wrap_content"  
  9.         android:layout_width="fill_parent" android:paddingLeft="4dip"  
  10.         android:paddingRight="4dip" android:paddingBottom="1dip"  
  11.         android:paddingTop="5dip" android:background="@android:drawable/bottom_bar"  
  12.  android:id="@+id/app_footer">  
  13.         <Button android:layout_weight="1" android:id="@+id/btn_export"  
  14.             android:layout_width="0dip" android:layout_height="fill_parent"  
  15.             android:text="@string/clear"></Button>  
  16.         <Button android:layout_weight="1" android:id="@+id/btn_sel_all"  
  17.             android:layout_width="0dip" android:layout_height="fill_parent"  
  18.             android:text="select_all"></Button>  
  19.         <Button android:layout_weight="1" android:id="@+id/btn_desel_all"  
  20.             android:layout_width="0dip" android:layout_height="fill_parent"  
  21.             android:text="deselect_all"></Button>  
  22.     </LinearLayout>  
  23.   
  24. </LinearLayout>  

 

BrowserInfo用于提供一个项的信息

 

Java代码   收藏代码
  1. public class BrowserInfo {  
  2.   
  3.     private int appInfoId;  
  4.     private int imageId;  
  5.     private boolean checked;  
  6.   
  7.     public BrowserInfo(int appInfoId, int imageId, boolean checked) {  
  8.         this.appInfoId = appInfoId;  
  9.         this.imageId = imageId;  
  10.         this.checked = checked;  
  11.     }  
  12.   
  13.     public boolean isChecked() {  
  14.         return checked;  
  15.     }  
  16.   
  17.     public void setChecked(boolean checked) {  
  18.         this.checked = checked;  
  19.     }  
  20.   
  21.     public int getAppInfoId() {  
  22.         return appInfoId;  
  23.     }  
  24.   
  25.     public void setAppInfoId(int appInfoId) {  
  26.         this.appInfoId = appInfoId;  
  27.     }  
  28.   
  29.     public int getImageId() {  
  30.         return imageId;  
  31.     }  
  32.   
  33.     public void setImageId(int imageId) {  
  34.         this.imageId = imageId;  
  35.     }  
  36.   
  37. }  

 

GroupInfo 用于显示一个组的信息

 

Java代码   收藏代码
  1. import java.util.List;  
  2.   
  3. public class GroupInfo {  
  4.   
  5.     private BrowserInfo group;  
  6.     private List<BrowserInfo> child;  
  7.   
  8.     public GroupInfo(BrowserInfo group, List<BrowserInfo> child) {  
  9.   
  10.         this.group = group;  
  11.         this.child = child;  
  12.     }  
  13.       
  14.     public void add(BrowserInfo info){  
  15.         child.add(info);  
  16.     }  
  17.       
  18.     public void remove(BrowserInfo info){  
  19.         child.remove(info);  
  20.     }  
  21.       
  22.     public void remove(int index){  
  23.         child.remove(index);  
  24.     }  
  25.       
  26.     public int getChildSize(){  
  27.         return child.size();  
  28.     }  
  29.       
  30.     public BrowserInfo getChild(int index){  
  31.         return child.get(index);  
  32.     }  
  33.   
  34.     public BrowserInfo getBrowserInfo() {  
  35.         return group;  
  36.     }  
  37.   
  38.     public void setBrowserInfo(BrowserInfo group) {  
  39.         this.group = group;  
  40.     }  
  41.   
  42.     public List<BrowserInfo> getChild() {  
  43.         return child;  
  44.     }  
  45.   
  46.     public void setChild(List<BrowserInfo> child) {  
  47.         this.child = child;  
  48.     }  
  49.   
  50. }  

 

 

ClearBrowserActivity 最后就是把要显示的内容显示出来的。

 

 

Java代码   收藏代码
  1. public class ClearBrowserActivity extends Activity implements ExpandableListView.OnGroupClickListener,ExpandableListView.OnChildClickListener{  
  2.   
  3.     private List<GroupInfo> group;  
  4.   
  5.     private ClearExpandableListAdapter listAdapter = null;  
  6.     private ExpandableListView appList;  
  7.   
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.browserlayout);  
  11.         appList = (ExpandableListView) findViewById(R.id.appList);  
  12.         init();  
  13.         BrowserInfo browsingHistoryParents = newBrowserInfo(  
  14.                 R.string.browsinghistory, R.drawable.browser_image,true);  
  15.         List<BrowserInfo> browsingHistoryChild = new ArrayList<BrowserInfo>();  
  16.         BrowserInfo browser = newBrowserInfo(R.string.browser,  
  17.                 R.drawable.browser_image, true);  
  18.         browsingHistoryChild.add(browser);  
  19.   
  20.         BrowserInfo operaClear = newBrowserInfo(R.string.opera_clear,  
  21.                 R.drawable.browser_image,true);  
  22.         browsingHistoryChild.add(operaClear);  
  23.           
  24.         BrowserInfo firefoxClear = newBrowserInfo(R.string.firefox_clear,  
  25.                 R.drawable.browser_image,true);  
  26.         browsingHistoryChild.add(firefoxClear);  
  27.           
  28.         BrowserInfo ucwebClear = newBrowserInfo(R.string.ucweb_clear,  
  29.                 R.drawable.browser_image,false);  
  30.         browsingHistoryChild.add(ucwebClear);  
  31.           
  32.         GroupInfo browserGroup = new GroupInfo(browsingHistoryParents,  
  33.                 browsingHistoryChild);  
  34.         addGroup(browserGroup);  
  35.   
  36.         listAdapter = new ClearExpandableListAdapter(this, group);  
  37.         appList.setOnChildClickListener(this);  
  38.         appList.setOnGroupClickListener(this);  
  39.         appList.setAdapter(listAdapter);  
  40.     }  
  41.   
  42.     private void init() {  
  43.         group = new ArrayList<GroupInfo>();  
  44.     }  
  45.   
  46.     private void addGroup(GroupInfo info) {  
  47.         group.add(info);  
  48.     }  
  49.   
  50.     private static BrowserInfo newBrowserInfo(int infoId, int imageId,boolean checked) {  
  51.         return new BrowserInfo(infoId, imageId,checked);  
  52.     }  
  53.   
  54.     @Override  
  55.     public boolean onGroupClick(ExpandableListView parent, View v,  
  56.             int groupPosition, long id) {  
  57.         return false;  
  58.     }  
  59.   
  60.     @Override  
  61.     public boolean onChildClick(ExpandableListView parent, View v,  
  62.             int groupPosition, int childPosition, long id) {  
  63.         return false;  
  64.     }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值