android ExpandableListView详解
Posted 火龙裸先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android ExpandableListView详解相关的知识,希望对你有一定的参考价值。
ExpandableListView是android中可以实现下拉list的一个控件,是一个垂直滚动的心事两个级别列表项手风琴试图,列表项是来自ExpandableListViewaAdapter,组可以单独展开。
重要方法:
1 expandGroup (int groupPos) ;//在分组列表视图中 展开一组, 2 setSelectedGroup (int groupPosition) ;//设置选择指定的组。 3 4 setSelectedChild (int groupPosition, int childPosition, boolean shouldExpandGroup);//设置选择指定的子项。 5 6 getPackedPositionGroup (long packedPosition);//返回所选择的组 7 8 getPackedPositionForChild (int groupPosition, int childPosition) ;//返回所选择的子项 9 10 getPackedPositionType (long packedPosition);//返回所选择项的类型(Child,Group) 11 12 isGroupExpanded (int groupPosition);//判断此组是否展开
expandableListView.setDivider();这个是设定每个Group之间的分割线。
expandableListView.setGroupIndicator();这个是设定每个Group之前的那个图标。
expandableListView.collapseGroup(int group); 将第group组收起
ExpandableListAdapter
一个接口,将基础数据链接到一个ExpandableListView。 此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。
1.重要方法
getChildId (int groupPosition, int childPosition) 获取与在给定组给予孩子相关的数据。
getChildrenCount (int groupPosition) 返回在指定Group的Child数目。
案例:
首先定义个一个布局文件expandablelistview.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" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 <ExpandableListView 7 android:id ="@+id/expandableListView" 8 android:layout_width ="fill_parent" 9 android:layout_height ="wrap_content" 10 > 11 </ExpandableListView> 12 </LinearLayout>
1 package com.test; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.security.auth.PrivateCredentialPermission; 7 8 import android.app.Activity; 9 import android.os.Bundle; 10 import android.view.Gravity; 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.view.Window; 14 import android.widget.AbsListView; 15 import android.widget.BaseExpandableListAdapter; 16 import android.widget.ExpandableListView; 17 import android.widget.TextView; 18 19 public class ExpandableListViewDemo extends Activity { 20 /** Called when the activity is first created. */ 21 22 //定义两个List用来控制Group和Child中的String; 23 24 private List<String> groupArray;//组列表 25 private List<List<String>> childArray;//子列表 26 private ExpandableListView expandableListView_one; 27 28 @Override 29 public void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 // requestWindowFeature(Window.FEATURE_NO_TITLE); //设置为无标题 32 setContentView(R.layout.expandablelistview); 33 expandableListView_one =(ExpandableListView)findViewById(R.id.expandableListView); 34 groupArray =new ArrayList<String>(); 35 childArray = new ArrayList<List<String>>(); 36 37 /*-第一季-*/ 38 initdate(); 39 expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this)); 40 41 /*-第二季-*/ 42 // groupArray.add("移动开发"); 43 // List<String> arrayList = new ArrayList<String>(); 44 // arrayList.add("Android"); 45 // arrayList.add("ios"); 46 // arrayList.add("Windows Phone"); 47 // //组循环 48 // for(int index=0;index<groupArray.size();++index) 49 // { 50 // childArray.add(arrayList); 51 // } 52 // expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this)); 53 54 } 55 class ExpandableListViewaAdapter extends BaseExpandableListAdapter { 56 Activity activity; 57 public ExpandableListViewaAdapter(Activity a) 58 { 59 activity = a; 60 } 61 /*-----------------Child */ 62 @Override 63 public Object getChild(int groupPosition, int childPosition) { 64 // TODO Auto-generated method stub 65 return childArray.get(groupPosition).get(childPosition); 66 } 67 68 @Override 69 public long getChildId(int groupPosition, int childPosition) { 70 // TODO Auto-generated method stub 71 return childPosition; 72 } 73 74 @Override 75 public View getChildView(int groupPosition, int childPosition, 76 boolean isLastChild, View convertView, ViewGroup parent) { 77 78 String string =childArray.get(groupPosition).get(childPosition); 79 80 return getGenericView(string); 81 } 82 83 @Override 84 public int getChildrenCount(int groupPosition) { 85 // TODO Auto-generated method stub 86 return childArray.get(groupPosition).size(); 87 } 88 /* ----------------------------Group */ 89 @Override 90 public Object getGroup(int groupPosition) { 91 // TODO Auto-generated method stub 92 return getGroup(groupPosition); 93 } 94 95 @Override 96 public int getGroupCount() { 97 // TODO Auto-generated method stub 98 return groupArray.size(); 99 } 100 101 @Override 102 public long getGroupId(int groupPosition) { 103 // TODO Auto-generated method stub 104 return groupPosition; 105 } 106 107 @Override 108 public View getGroupView(int groupPosition, boolean isExpanded, 109 View convertView, ViewGroup parent) { 110 111 String string=groupArray.get(groupPosition); 112 return getGenericView(string); 113 } 114 115 @Override 116 public boolean hasStableIds() { 117 // TODO Auto-generated method stub 118 return false; 119 } 120 121 @Override 122 public boolean isChildSelectable(int groupPosition, int childPosition) 123 { 124 // TODO Auto-generated method stub 125 return true; 126 } 127 128 private TextView getGenericView(String string ) 129 { 130 AbsListView.LayoutParams layoutParams =new AbsListView.LayoutParams( 131 ViewGroup.LayoutParams.MATCH_PARENT, 132 ViewGroup.LayoutParams.WRAP_CONTENT); 133 134 TextView textView =new TextView(activity); 135 textView.setLayoutParams(layoutParams); 136 137 textView.setGravity(Gravity.CENTER_VERTICAL |Gravity.LEFT); 138 139 textView.setPadding(40, 0, 0, 0); 140 textView.setText(string); 141 return textView; 142 } 143 } 144 145 private void initdate() 146 { 147 addInfo("语言", new String[]{"Oracle","Java","Linux","Jquery"}); 148 addInfo("男人的需求", new String[]{"金钱","事业","权力","女人","房子","车","球"}); 149 } 150 private void addInfo(String group,String []child) { 151 152 groupArray.add(group); 153 154 List<String> childItem =new ArrayList<String>(); 155 156 for(int index=0;index<child.length;index++) 157 { 158 childItem.add(child[index]); 159 } 160 childArray.add(childItem); 161 } 162 }
以上是关于android ExpandableListView详解的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )