android ExpandAbleListView控件

ExpandAbleListView控件

1.API对ExpandAbleListView的解释:

这里主要的意思就是说:ExpandAbleListView类似于ListView ,但它可以每行展开出自己的list;

2.重要方法

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

3.下面直接展示一个例子

效果如下:

xml文件中就是一个ExpandAbleListView

MainActivity:

  1 public class MainActivity extends Activity {
  2 
  3     private ExpandableListView expandAbleListView;
  4 
  5     private List<String> groupArray;
  6 
  7     private List<List<String>> childArray;
  8 
  9     @Override
 10     protected void onCreate(Bundle savedInstanceState) {
 11         super.onCreate(savedInstanceState);
 12         setContentView(R.layout.activity_main);
 13         groupArray = new ArrayList<String>();
 14         childArray = new ArrayList<List<String>>();
 15         groupArray.add("水果");
 16         groupArray.add("动物");
 17         List<String> tempArray = new ArrayList<String>();
 18         tempArray.add("葡萄");
 19         tempArray.add("樱桃");
 20         tempArray.add("苹果");
 21         List<String> tempArray1 = new ArrayList<String>();
 22         tempArray1.add("猴子");
 23         tempArray1.add("熊猫");
 24         tempArray1.add("大象");
 25         childArray.add(tempArray);
 26         childArray.add(tempArray1);
 27 
 28         expandAbleListView = (ExpandableListView) findViewById(R.id.expandAbleListView);
 29         expandAbleListView.setAdapter(new myExpandAbleListAdapter(
 30                 MainActivity.this));
 31         expandAbleListView
 32                 .setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
 33 
 34                     @Override
 35                     public boolean onChildClick(ExpandableListView parent,
 36                             View v, int groupPosition, int childPosition,
 37                             long id) {
 38                         System.out.println("onChildClick");
 39                         if (groupPosition == 0) {
 40                             TextView childView = (TextView) v;
 41                             childView.setText("第一组group的文字改变");
 42 
 43                         }
 44 
 45                         return false;
 46                     }
 47                 });
 48 
 49         // 如果要点击展开一个group另外的group格式关闭的 给group添加展开监听,当其中一个group展开的时候其他的group就会关闭
 50         expandAbleListView
 51                 .setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
 52 
 53                     @Override
 54                     public void onGroupExpand(int groupPosition) {
 55                         for (int i = 0; i < groupArray.size(); i++) {
 56                             if (i != groupPosition) {
 57 
 58                                 expandAbleListView.collapseGroup(i);
 59 
 60                             }
 61 
 62                         }
 63                     }
 64                 });
 65 
 66     }
 67 
 68     // 自定义ExpandAbleListView
 69     class myExpandAbleListAdapter extends BaseExpandableListAdapter {
 70         Context context;
 71 
 72         public myExpandAbleListAdapter(Context context1) {
 73             this.context = context1;
 74 
 75         }
 76 
 77         @Override
 78         public int getGroupCount() {
 79             return groupArray.size();
 80         }
 81 
 82         @Override
 83         public int getChildrenCount(int groupPosition) {
 84             return childArray.get(groupPosition).size();
 85         }
 86 
 87         @Override
 88         public Object getGroup(int groupPosition) {
 89             return groupArray.get(groupPosition);
 90         }
 91 
 92         @Override
 93         public Object getChild(int groupPosition, int childPosition) {
 94             return childArray.get(groupPosition).get(childPosition);
 95         }
 96 
 97         @Override
 98         public long getGroupId(int groupPosition) {
 99 
100             return groupPosition;
101         }
102 
103         private TextView obtainTextView(String str) {
104             AbsListView.LayoutParams params = new AbsListView.LayoutParams(
105                     ViewGroup.LayoutParams.FILL_PARENT, 64);
106             TextView textView = new TextView(context);
107             textView.setLayoutParams(params);
108             textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
109             // Set the text starting position
110             textView.setPadding(36, 0, 0, 0);
111             textView.setText(str);
112             return textView;
113 
114         }
115 
116         @Override
117         public long getChildId(int groupPosition, int childPosition) {
118             return childPosition;
119         }
120 
121         @Override
122         public boolean hasStableIds() {
123             return false;
124         }
125 
126         // 获取父list
127         @Override
128         public View getGroupView(int groupPosition, boolean isExpanded,
129                 View convertView, ViewGroup parent) {
130 
131             TextView textView = obtainTextView(groupArray.get(groupPosition));
132             //
133             // if (isExpanded) {
134             //
135             // Toast.makeText(context, "父类是展开了的", Toast.LENGTH_SHORT).show();
136             // } else {
137             //
138             // Toast.makeText(context, "子类是展开了的", Toast.LENGTH_SHORT).show();
139             // }
140 
141             return textView;
142         }
143 
144         // 获取子list
145         @Override
146         public View getChildView(int groupPosition, int childPosition,
147                 boolean isLastChild, View convertView, ViewGroup parent) {
148             TextView textView = obtainTextView(childArray.get(groupPosition)
149                     .get(childPosition));
150             textView.setGravity(Gravity.CENTER_HORIZONTAL);
151 
152             return textView;
153         }
154 
155         // 如果想让childview能被点击就要返回true
156         @Override
157         public boolean isChildSelectable(int groupPosition, int childPosition) {
158             return true;
159         }
160 
161     }
162 
163 }

源码

 

这个简单的例子就到此,下次实现一个综合点的例子如图:

posted @ 2014-06-14 22:15  perfect亮  阅读(422)  评论(0编辑  收藏  举报