折叠列表

实现效果:

即可伸展的ListView



其实跟普通的ListView使用没啥区别,只是ListView改为了ExpandableListView,另外适配器由BaseAdapter也换成了BaseExpandableListAdapter。

步骤:

1.编写布局文件。

分为三个,分别是主布局,group分组布局,group item布局:
activity_main.xml:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.example.expandablelistviewdemo.MainActivity" >  
  6.     <ExpandableListView  
  7.         android:id="@+id/elv"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:divider="#000"  
  11.         android:dividerHeight="2.5dp"  
  12.         android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft" >  
  13.     </ExpandableListView>  
  14. </RelativeLayout>  

list_group.xml:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:id="@+id/listTitle"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:paddingBottom="10dp"  
  11.         android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"  
  12.         android:paddingTop="10dp"  
  13.         android:textColor="#A4C739" />  
  14. </LinearLayout>  
list_item.xml:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:id="@+id/expandedListItem"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:paddingBottom="10dp"  
  11.         android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"  
  12.         android:paddingTop="10dp" />  
  13. </LinearLayout>  
2.编写适配器代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.expandablelistviewdemo;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import android.content.Context;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseExpandableListAdapter;  
  8. import android.widget.TextView;  
  9. public class MyExpandableListAdapter extends BaseExpandableListAdapter  
  10. {  
  11.     private Context mContext;  
  12.     private List<String> expandableListTitle;  
  13.     private HashMap<String, List<String>> expandableListDetail;  
  14.       
  15.     public MyExpandableListAdapter(List<String> expandableListTitle,HashMap<String,List<String>> expandableListDetail,Context mContext)  
  16.     {  
  17.         this.expandableListDetail = expandableListDetail;  
  18.         this.expandableListTitle = expandableListTitle;  
  19.         this.mContext = mContext;  
  20.     }  
  21.       
  22.     @Override  
  23.     public int getGroupCount()//分组数  
  24.     {  
  25.         return this.expandableListTitle.size();  
  26.     }  
  27.     @Override  
  28.     public int getChildrenCount(int groupPosition)//分组内的item数  
  29.     {  
  30.         return this.expandableListDetail.get(expandableListTitle.get(groupPosition)).size();  
  31.     }  
  32.     @Override  
  33.     public Object getGroup(int groupPosition)//获取分组数据  
  34.     {  
  35.         return this.expandableListTitle.get(groupPosition);  
  36.     }  
  37.     @Override  
  38.     public Object getChild(int groupPosition, int childPosition)//获取第几分组第几个item的数据  
  39.     {  
  40.         return this.expandableListDetail.get(this.expandableListTitle.get(groupPosition)).get(childPosition);  
  41.     }  
  42.     @Override  
  43.     public long getGroupId(int groupPosition)  
  44.     {  
  45.         return groupPosition;  
  46.     }  
  47.     @Override  
  48.     public long getChildId(int groupPosition, int childPosition)  
  49.     {  
  50.         return childPosition;  
  51.     }  
  52.     @Override  
  53.     public boolean hasStableIds()  
  54.     {  
  55.         return false;  
  56.     }  
  57.     @Override  
  58.     public View getGroupView(int groupPosition, boolean isExpanded,  
  59.             View convertView, ViewGroup parent)  
  60.     {  
  61.         String data = this.expandableListTitle.get(groupPosition);  
  62.         if(convertView == null)  
  63.         {  
  64.             convertView = View.inflate(mContext,R.layout.list_group, null);  
  65.         }  
  66.         TextView tv = (TextView) convertView.findViewById(R.id.listTitle);  
  67.         tv.setText(data);  
  68.         return convertView;  
  69.     }  
  70.     @Override  
  71.     public View getChildView(int groupPosition, int childPosition,  
  72.             boolean isLastChild, View convertView, ViewGroup parent)  
  73.     {  
  74. //      String data = this.expandableListDetail.get(this.expandableListTitle.get(groupPosition)).get(childPosition);  
  75.         String data = (String) this.getChild(groupPosition, childPosition);  
  76.         if(convertView == null)  
  77.         {  
  78.             convertView = View.inflate(mContext, R.layout.list_item, null);  
  79.         }  
  80.         TextView tv = (TextView) convertView.findViewById(R.id.expandedListItem);  
  81.         tv.setText(data);  
  82.         return convertView;  
  83.     }  
  84.     @Override  
  85.     public boolean isChildSelectable(int groupPosition, int childPosition)  
  86.     {  
  87.         return true;  
  88.     }  
  89. }  

可以看到,适配器中有两个getView的方法,分别去构造group和child的view。

3.构造数据源
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.expandablelistviewdemo;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. public class ExpandableListDataPump  
  6. {  
  7.     public static HashMap<String, List<String>> getData()  
  8.     {  
  9.         HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();  
  10.         List<String> technology = new ArrayList<String>();  
  11.         technology.add("Beats sued for noise-cancelling tech");  
  12.         technology.add("Wikipedia blocks US Congress edits");  
  13.         technology.add("Google quizzed over deleted links");  
  14.         technology.add("Nasa seeks aid with Earth-Mars links");  
  15.         technology.add("The Good, the Bad and the Ugly");  
  16.         List<String> entertainment = new ArrayList<String>();  
  17.         entertainment.add("Goldfinch novel set for big screen");  
  18.         entertainment.add("Anderson stellar in Streetcar");  
  19.         entertainment.add("Ronstadt receives White House honour");  
  20.         entertainment.add("Toronto to open with The Judge");  
  21.         entertainment.add("British dancer return from Russia");  
  22.         List<String> science = new ArrayList<String>();  
  23.         science.add("Eggshell may act like sunblock");  
  24.         science.add("Brain hub predicts negative events");  
  25.         science.add("California hit by raging wildfires");  
  26.         science.add("Rosetta's comet seen in close-up");  
  27.         science.add("Secret of sandstone shapes revealed");  
  28.         expandableListDetail.put("TECHNOLOGY NEWS", technology);  
  29.         expandableListDetail.put("ENTERTAINMENT NEWS", entertainment);  
  30.         expandableListDetail.put("SCIENCE & ENVIRONMENT NEWS", science);  
  31.         return expandableListDetail;  
  32.     }  
  33. }  

4.编写主界面的代码
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.expandablelistviewdemo;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import android.annotation.SuppressLint;  
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.ExpandableListView;  
  10. import android.widget.ExpandableListView.OnChildClickListener;  
  11. import android.widget.ExpandableListView.OnGroupCollapseListener;  
  12. import android.widget.ExpandableListView.OnGroupExpandListener;  
  13. import android.widget.Toast;  
  14. public class MainActivity extends Activity  
  15. {  
  16.     private ExpandableListView elv = null;  
  17.     private MyExpandableListAdapter adapter = null;  
  18.       
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState)  
  21.     {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.           
  25.         elv = (ExpandableListView) findViewById(R.id.elv);  
  26.           
  27.         final HashMap<String,List<String>> itemData = ExpandableListDataPump.getData();  
  28.         final List<String> title = new ArrayList<String>(itemData.keySet());  
  29.         adapter = new MyExpandableListAdapter(title,itemData,this);  
  30.           
  31.         elv.setAdapter(adapter);  
  32.           
  33.         //去掉箭头  
  34. //      elv.setGroupIndicator(null);  
  35.           
  36.         //收缩  
  37.         elv.setOnGroupCollapseListener(new OnGroupCollapseListener()  
  38.         {  
  39.               
  40.             @Override  
  41.             public void onGroupCollapse(int groupPosition)  
  42.             {  
  43.                 Toast.makeText(MainActivity.this,title.get(groupPosition)+" group collapse..... "0).show();  
  44.             }  
  45.         });  
  46.         //伸展  
  47.         elv.setOnGroupExpandListener(new OnGroupExpandListener()  
  48.         {  
  49.             @Override  
  50.             public void onGroupExpand(int groupPosition)  
  51.             {  
  52.                 Toast.makeText(MainActivity.this,title.get(groupPosition)+" group expand... "0).show();  
  53.             }  
  54.         });  
  55.         //子条目点击  
  56.         elv.setOnChildClickListener(new OnChildClickListener()  
  57.         {  
  58.             @Override  
  59.             public boolean onChildClick(ExpandableListView parent, View v,  
  60.                     int groupPosition, int childPosition, long id)  
  61.             {  
  62.                 Toast.makeText(MainActivity.this,itemData.get(title.get(groupPosition)).get(childPosition)+" click"0).show();  
  63.                 return false;  
  64.             }  
  65.         });  
  66.           
  67.     }  
  68. }  

ok,完成。
这里需要注意的是ExpandableListView有多个监听器,group收缩和伸展以及子条目点击等等,按需监听。
另外,默认情况下显示的时候有个箭头,你可以通过setGroupIndicator方法设置你需要的标识。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值