Android之ExpandableListView下拉分组的实现

简介: ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView view plaincopy to clipboardprint? <LinearLayout       android:id="@+id/linearLayout" 

ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下:

首先:在layout的xml文件中定义一个ExpandableListView

  1. <LinearLayout   
  2.     android:id="@+id/linearLayout"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="fill_parent"  
  5.     androidrientation="vertical"  
  6.     >  
  7.       
  8.     <ExpandableListView  
  9.     android:id="@+id/expandableListView"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="wrap_content"  
  12.         />  
  13. </LinearLayout>  
 

定义两个List,用来存放控件中Group/Child中的String

  1. private List<String> groupArray;  
  2. private List<List<String>> childArray;  
 

对这两个List进行初始化,并插入一些数据

  1. groupArray = new ArrayList<String>();  
  2. childArray = new ArrayList<List<String>>();  
  3.   
  4. groupArray.add("第一行");  
  5. groupArray.add("第二行");  
  6.   
  7. List<String> tempArray = new ArrayList<String>();  
  8. tempArray.add("第一条");  
  9. tempArray.add("第二条");  
  10. tempArray.add("第三条");  
  11.   
  12. for(int index = 0; index <groupArray.size(); ++index)  
  13. {  
  14.     childArray.add(tempArray);  
  15. }  

定义ExpandableListView的Adapter

  1. //ExpandableListView的Adapter  
  2. public class ExpandableAdapter extends BaseExpandableListAdapter  
  3. {  
  4.     Activity activity;  
  5.       
  6.     public ExpandableAdapter(Activity a)  
  7.     {  
  8.         activity = a;  
  9.     }  
  10.     public Object getChild(int groupPosition, int childPosition)  
  11.     {  
  12.         return childArray.get(groupPosition).get(childPosition);  
  13.     }  
  14.     public long getChildId(int groupPosition, int childPosition)  
  15.     {  
  16.         return childPosition;  
  17.     }  
  18.     public int getChildrenCount(int groupPosition)  
  19.     {  
  20.         return childArray.get(groupPosition).size();  
  21.     }  
  22.     public View getChildView(int groupPosition, int childPosition,  
  23.             boolean isLastChild, View convertView, ViewGroup parent)  
  24.     {  
  25.         String string = childArray.get(groupPosition).get(childPosition);  
  26.         return getGenericView(string);  
  27.     }  
  28.     // group method stub  
  29.     public Object getGroup(int groupPosition)  
  30.     {  
  31.         return groupArray.get(groupPosition);  
  32.     }  
  33.     public int getGroupCount()  
  34.     {  
  35.         return groupArray.size();  
  36.     }  
  37.     public long getGroupId(int groupPosition)  
  38.     {  
  39.         return groupPosition;  
  40.     }  
  41.     public View getGroupView(int groupPosition, boolean isExpanded,  
  42.             View convertView, ViewGroup parent)  
  43.     {  
  44.         String string = groupArray.get(groupPosition);  
  45.         return getGenericView(string);  
  46.     }  
  47.     // View stub to create Group/Children 's View  
  48.     public TextView getGenericView(String string)  
  49.     {  
  50.         // Layout parameters for the ExpandableListView  
  51.         AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(  
  52.                 ViewGroup.LayoutParams.FILL_PARENT, 64);  
  53.         TextView text = new TextView(activity);  
  54.         text.setLayoutParams(layoutParams);  
  55.         // Center the text vertically  
  56.         text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  57.         // Set the text starting position  
  58.         text.setPadding(36000);  
  59.         text.setText(string);  
  60.         return text;  
  61.     }  
  62.     public boolean hasStableIds()  
  63.     {  
  64.         return false;  
  65.     }  
  66.     public boolean isChildSelectable(int groupPosition, int childPosition)  
  67.     {  
  68.         return true;  
  69.     }  
  70. }  

最后,给定义好的ExpandableListView添加上Adapter

  1. ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);  
  2. expandableListView.setAdapter(new ExpandableAdapter(Main.this));  

运行即可见效果~~~

 

----------------------------------------------------------------------------------------------------------------

Android版手风琴(ExpandableListView)

先看效果,过瘾一番。

 

 

 源码下载:http://files.cnblogs.com/salam/WidgetDemo.rar

 

  

  ExpandableListView是Android中的手风琴,本人感觉效果相当棒。

  一、ExpandableListView介绍

    一个垂直滚动的显示两个级别(Child,Group)列表项的视图,列表项来自ExpandableListAdapter 。组可以单独展开。

  1.重要方法

      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) :判断此组是否展开

  2.代码:

ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo)item.getMenuInfo();
  String title=((TextView)menuInfo.targetView).getText().toString();
  int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition);
  
  if (type==ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
  int groupPos =ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
  int childPos =ExpandableListView.getPackedPositionChild(menuInfo.packedPosition);

二、ExpandableListAdapter

    一个接口,将基础数据链接到一个ExpandableListView。此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。

  1.重要方法

    getChildId(int groupPosition, int childPosition) 获取与在给定组给予孩子相关的数据。

    getChildrenCount(int groupPosition) 返回在指定Group的Child数目。

  2.代码

 public class MyExpandableListAdapter extends BaseExpandableListAdapter {
         // Sample data set.  children[i] contains the children (String[]) for groups[i].
         public String[] groups = { "我的好友", "新疆同学", "亲戚", "同事" };
         public String[][] children = {
                 { "胡算林", "张俊峰", "王志军", "二人" },
                 { "李秀婷", "蔡乔", "别高", "余音" },
                 { "摊派新", "张爱明" },
                 { "马超", "司道光" }
         };
        
         public Object getChild(int groupPosition, int childPosition) {
             return children[groupPosition][childPosition];
         }

         public long getChildId(int groupPosition, int childPosition) {
             return childPosition;
         }

         public int getChildrenCount(int groupPosition) {
             return children[groupPosition].length;
         }

         public TextView getGenericView() {
             // Layout parameters for the ExpandableListView
             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                     ViewGroup.LayoutParams.MATCH_PARENT, 64);

             TextView textView = new TextView(ExpandableListDemo.this);
             textView.setLayoutParams(lp);
             // Center the text vertically
             textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
             // Set the text starting position
             textView.setPadding(36, 0, 0, 0);
             return textView;
         }
        
         public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                 View convertView, ViewGroup parent) {
             TextView textView = getGenericView();
             textView.setText(getChild(groupPosition, childPosition).toString());
             return textView;
         }

         public Object getGroup(int groupPosition) {
             return groups[groupPosition];
         }

         public int getGroupCount() {
             return groups.length;
         }

         public long getGroupId(int groupPosition) {
             return groupPosition;
         }

         public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                 ViewGroup parent) {
             TextView textView = getGenericView();
             textView.setText(getGroup(groupPosition).toString());
             return textView;
         }

         public boolean isChildSelectable(int groupPosition, int childPosition) {
             return true;
         }

         public boolean hasStableIds() {
             return true;
         }

     }

目录
相关文章
|
Android开发
flutter中实现仿Android端的onResume和onPause方法
flutter中实现仿Android端的onResume和onPause方法
|
10月前
|
Android开发
Android中下拉通知栏,Activity会走哪些生命周期?
我们就可以做一个总结:当前Activity中,下拉通知栏,是不走任何生命周期的。
155 0
|
11月前
|
XML Java Android开发
Android ExpandableListView 使用中遇到的问题集锦
Android ExpandableListView 使用中遇到的问题集锦
|
Java Shell API
Android源码(6.0和8.1) 屏蔽状态栏下拉和屏蔽导航栏显示
Android源码(6.0和8.1) 屏蔽状态栏下拉和屏蔽导航栏显示
412 0
|
Android开发
Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景
Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景
660 0
Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景
|
Android开发
Android 取消 ExpandableListView 的分割线,解决ScrollView 嵌套 ExpandableListView的问题
Android 取消 ExpandableListView 的分割线,解决ScrollView 嵌套 ExpandableListView的问题
|
Android开发 容器
Android实现面包屑效果,支持Fragment联动
Android实现面包屑效果,支持Fragment联动
|
Android开发
Android实现连线题效果
Android实现连线题效果
|
Android开发
Android实现调用系统相机录像及实现录音
Android实现调用系统相机录像及实现录音
591 0
|
移动开发 JavaScript Android开发
通过howler.js实现在Android下的微信浏览器自动播放音频
通过howler.js实现在Android下的微信浏览器自动播放音频
414 0
通过howler.js实现在Android下的微信浏览器自动播放音频