标签:des android style blog http io color ar os
最近在公司做项目,有一个小模块,需要用到ExpandableListView这个控件,所以在工作中边学习边工作。根据需求,在进入ExpandableLisView的时候默认展开第一组,需要用到这个属性,expandableListView.expandGroup(0);
在ExpandableListView中,当点击一个组展开时,其他展开的组是不自动关上的,这需要在监听事件中处理一下:
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
for (int i = 0, count = expandableListView.getExpandableListAdapter()
.getGroupCount(); i < count; i++) {
if (groupPosition != i) {// 关闭其他分组
expandableListView.collapseGroup(i);
}
}
}
还有一个地方需要注意的就是在Adapter的getChildrenCount(int groupPosition)方法,要return child.get(groupPosition).size(),否则会在点击组得时候,报数组越界的问题。
下边是部分代码:
MainActivity.java
1 package com.example.expandablelistviewdemo; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.widget.ExpandableListView; 8 import android.widget.ExpandableListView.OnGroupExpandListener; 9 10 public class MainActivity extends Activity implements OnGroupExpandListener { 11 private ExpandableListView expandableListView; 12 private ServiceStationAdapter adapter; 13 private ArrayList<String> groupList = new ArrayList<String>(); 14 private ArrayList<List<ServerStation>> childList = new ArrayList<List<ServerStation>>(); 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 // TODO Auto-generated method stub 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1); 22 23 setDate(); 24 } 25 26 /** 27 * 填充数据 28 * 29 */ 30 private void setDate() { 31 List<ServerStation> list1 = new ArrayList<ServerStation>(); 32 for (int i = 0; i < 5; i++) { 33 ServerStation serverStation = new ServerStation(); 34 serverStation.setAddressDiscri("北京丰台区甲23号" + "," + i); 35 serverStation.setFaxs("010-1234556" + "," + i); 36 serverStation.setPhones("010-74123412" + "," + i); 37 list1.add(serverStation); 38 } 39 40 addDate("北京", list1); 41 List<ServerStation> list2 = new ArrayList<ServerStation>(); 42 for (int i = 0; i < 3; i++) { 43 ServerStation serverStation = new ServerStation(); 44 serverStation.setAddressDiscri("河北省衡水市大风路23号5栋3门302室" + "," + i); 45 serverStation.setFaxs("0313-1234556" + "," + i); 46 serverStation.setPhones("0313-74123412" + "," + i); 47 list2.add(serverStation); 48 } 49 addDate("河北", list2); 50 expandableListView.setGroupIndicator(null); 51 adapter = new ServiceStationAdapter(MainActivity.this, groupList, 52 childList); 53 expandableListView.setAdapter(adapter); 54 // 展开一组,关闭其他组 55 expandableListView.setOnGroupExpandListener(MainActivity.this); 56 // 默认展开第一项 57 expandableListView.expandGroup(0); 58 59 } 60 61 @Override 62 public void onGroupExpand(int groupPosition) { 63 // TODO Auto-generated method stub 64 for (int i = 0, count = expandableListView.getExpandableListAdapter() 65 .getGroupCount(); i < count; i++) { 66 if (groupPosition != i) {// 关闭其他分组 67 expandableListView.collapseGroup(i); 68 } 69 } 70 } 71 72 /** 73 * 添加数据信息 74 * 75 * @param g 76 * @param c 77 */ 78 private void addDate(String g, List<ServerStation> c) { 79 groupList.add(g); 80 List<ServerStation> list = new ArrayList<ServerStation>(); 81 for (int i = 0; i < c.size(); i++) { 82 list.add(c.get(i)); 83 } 84 childList.add(list); 85 } 86 87 }
ServiceStationAdapter.java
1 package com.example.expandablelistviewdemo; 2 3 import java.util.List; 4 5 6 import android.app.AlertDialog; 7 import android.content.Context; 8 import android.content.DialogInterface; 9 import android.content.Intent; 10 import android.net.Uri; 11 import android.view.LayoutInflater; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.view.ViewGroup; 15 import android.widget.BaseExpandableListAdapter; 16 import android.widget.ImageView; 17 import android.widget.LinearLayout; 18 import android.widget.TextView; 19 20 /** 21 * expandableListView 22 * 23 */ 24 public class ServiceStationAdapter extends BaseExpandableListAdapter { 25 private Context context; 26 private List<String> group; 27 private List<List<ServerStation>> child; 28 29 public ServiceStationAdapter(Context context, List<String> group, 30 List<List<ServerStation>> child) { 31 this.context = context; 32 this.group = group; 33 this.child = child; 34 } 35 36 @Override 37 public int getGroupCount() { 38 return group.size(); 39 } 40 41 @Override 42 public int getChildrenCount(int groupPosition) { 43 // return child.size(); 44 return child.get(groupPosition).size(); 45 } 46 47 @Override 48 public Object getGroup(int groupPosition) { 49 return group.get(groupPosition); 50 } 51 52 @Override 53 public Object getChild(int groupPosition, int childPosition) { 54 return child.get(childPosition).get(childPosition); 55 } 56 57 @Override 58 public long getGroupId(int groupPosition) { 59 return groupPosition; 60 } 61 62 @Override 63 public long getChildId(int groupPosition, int childPosition) { 64 return childPosition; 65 } 66 67 @Override 68 public boolean hasStableIds() { 69 return false; 70 } 71 72 /** 73 * 修改listview组得样式 group 74 */ 75 @Override 76 public View getGroupView(int groupPosition, boolean isExpanded, 77 View convertView, ViewGroup parent) { 78 ViewHolder holder; 79 if (convertView == null) { 80 convertView = LayoutInflater.from(context).inflate( 81 R.layout.expandlistview_group_layout, null); 82 holder = new ViewHolder(); 83 holder.tvCity = (TextView) convertView.findViewById(R.id.tv_city); 84 holder.imgArrow = (ImageView) convertView 85 .findViewById(R.id.imageView); 86 convertView.setTag(holder); 87 } else { 88 holder = (ViewHolder) convertView.getTag(); 89 } 90 if (isExpanded) { 91 holder.imgArrow 92 .setBackgroundResource(R.drawable.down_selected_icon); 93 } else { 94 holder.imgArrow.setBackgroundResource(R.drawable.up_selected_icon); 95 } 96 holder.tvCity.setText(group.get(groupPosition)); 97 return convertView; 98 99 } 100 101 /** 102 * child 103 */ 104 @Override 105 public View getChildView(int groupPosition, int childPosition, 106 boolean isLastChild, View convertView, ViewGroup parent) { 107 ViewHolder holder; 108 if (convertView == null) { 109 convertView = LayoutInflater.from(context).inflate( 110 R.layout.expandlistview_child_layout, null); 111 holder = new ViewHolder(); 112 holder.tvAddress = (TextView) convertView 113 .findViewById(R.id.tv_address); 114 holder.tvFax = (TextView) convertView.findViewById(R.id.tv_fax); 115 holder.tvTel = (TextView) convertView.findViewById(R.id.tv_tel); 116 holder.telLayout = (LinearLayout) convertView 117 .findViewById(R.id.tel_layout); 118 convertView.setTag(holder); 119 } else { 120 holder = (ViewHolder) convertView.getTag(); 121 } 122 String address = child.get(groupPosition).get(childPosition) 123 .getAddressDiscri(); 124 holder.tvAddress.setText(address); 125 String fax = child.get(groupPosition).get(childPosition).getFaxs(); 126 if ("".equals(fax)) { 127 holder.tvFax.setText("暂无传真信息"); 128 } else { 129 holder.tvFax.setText(fax); 130 } 131 132 String mobile = child.get(groupPosition).get(childPosition).getPhones(); 133 if ("".equals(mobile)) { 134 holder.tvTel.setText("暂无电话信息"); 135 } else { 136 holder.tvTel.setText(mobile); 137 } 138 139 holder.telLayout.setOnClickListener(new MyclickListener(groupPosition, 140 childPosition)); 141 return convertView; 142 } 143 144 class ViewHolder { 145 TextView tvCity, tvAddress, tvFax, tvTel; 146 ImageView imgArrow; 147 LinearLayout telLayout; 148 149 } 150 151 @Override 152 public boolean isChildSelectable(int groupPosition, int childPosition) { 153 return false; 154 } 155 156 class MyclickListener implements OnClickListener { 157 int groupPosition, childPosition; 158 159 public MyclickListener(int groupIndex, int childIndex) { 160 // TODO Auto-generated constructor stub 161 162 this.groupPosition = groupIndex; 163 this.childPosition = childIndex; 164 } 165 166 @Override 167 public void onClick(View v) { 168 169 // showCustomerService(child.get(groupPosition).get(childPosition) 170 // .getTel()); 171 showDialog(child.get(groupPosition).get(childPosition).getPhones(), 172 "拨打电话"); 173 } 174 } 175 176 /** 177 * 178 * @Title: showDialog 179 * @Description: TODO(这里用一句话描述这个方法的作用) 180 * @param: @param visible 控件的显隐 181 * @param: @param message dialog内容显示 182 * @param: @param titleStr dialog的标题 183 * @param: @param id 根据id不同设置dialog确定按钮显示 184 * @return: void 返回类型 185 * @date: 2014-9-17 下午4:40:10 186 */ 187 public void showDialog(final String message, final String titleStr) { 188 AlertDialog.Builder builder = new AlertDialog.Builder(context); 189 builder.setTitle(titleStr); 190 builder.setMessage(message); 191 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 192 193 @Override 194 public void onClick(DialogInterface dialog, int which) { 195 // TODO Auto-generated method stub 196 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" 197 + message)); 198 context.startActivity(intent); 199 dialog.dismiss(); 200 } 201 202 }); 203 204 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 205 206 @Override 207 public void onClick(DialogInterface dialog, int which) { 208 // TODO Auto-generated method stub 209 dialog.dismiss(); 210 } 211 }); 212 213 builder.create().show(); 214 } 215 216 }
ServiceStation.java
1 package com.example.expandablelistviewdemo; 2 3 import java.io.Serializable; 4 5 6 public class ServerStation implements Serializable { 7 /** 8 */ 9 private static final long serialVersionUID = 7492110209741256694L; 10 private String addressDiscri = "";// 详细地址 11 private String phones = "";// 电话 12 private String faxs = "";// 传真 13 public String getAddressDiscri() { 14 return addressDiscri; 15 } 16 17 public void setAddressDiscri(String addressDiscri) { 18 this.addressDiscri = addressDiscri; 19 } 20 21 public String getPhones() { 22 return phones; 23 } 24 25 public void setPhones(String phones) { 26 this.phones = phones; 27 } 28 29 public String getFaxs() { 30 return faxs; 31 } 32 33 public void setFaxs(String faxs) { 34 this.faxs = faxs; 35 } 36 }
布局文件
activity_main.xml
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:background="#f9f9f9" 6 android:orientation="vertical" > 7 8 <ExpandableListView 9 android:id="@+id/expandableListView1" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" > 12 </ExpandableListView> 13 14 </LinearLayout>
expandlistview_group_layout.xml
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 7 <RelativeLayout 8 android:padding="10dp" 9 android:gravity="center_vertical" 10 android:layout_width="match_parent" 11 android:layout_height="40dp" > 12 13 <TextView 14 android:textColor="@android:color/black" 15 android:textSize="10sp" 16 android:id="@+id/tv_city" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_alignParentLeft="true" 20 android:text="北京" /> 21 22 <ImageView 23 android:id="@+id/imageView" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_alignParentRight="true" 27 android:layout_centerVertical="true" /> 28 29 </RelativeLayout> 30 31 </LinearLayout>
expandlistview_child_layout.xml
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:background="#6f737d" 6 android:orientation="vertical" > 7 8 <TextView 9 android:id="@+id/tv_address" 10 android:layout_width="match_parent" 11 android:layout_height="50dp" 12 android:layout_marginLeft="10dp" 13 android:gravity="center_vertical" 14 android:textColor="@android:color/white" /> 15 16 <LinearLayout 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:gravity="center_vertical" 20 21 android:orientation="horizontal" > 22 23 <LinearLayout 24 android:layout_width="fill_parent" 25 android:layout_height="match_parent" 26 android:layout_weight="1" 27 android:gravity="center_vertical" 28 android:orientation="horizontal" 29 android:padding="10dp" > 30 31 <ImageView 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:src="@drawable/fax_icon" /> 35 36 <TextView 37 android:id="@+id/tv_fax" 38 android:layout_width="100dp" 39 android:layout_height="wrap_content" 40 android:layout_marginLeft="10dp" 41 android:textColor="@android:color/white" /> 42 </LinearLayout> 43 44 <LinearLayout 45 android:id="@+id/tel_layout" 46 android:layout_width="fill_parent" 47 android:layout_height="match_parent" 48 android:layout_weight="1" 49 android:background="@drawable/station_tel_selected" 50 android:gravity="center_vertical" 51 android:orientation="horizontal" 52 android:padding="10dp" > 53 54 <ImageView 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 android:src=<?xml version="1.0" encoding="utf-8"?> 58 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 59 android:layout_width="match_parent" 60 android:layout_height="match_parent" 61 android:background="#6f737d" 62 android:orientation="vertical" > 63 64 <TextView 65 android:id="@+id/tv_address" 66 android:layout_width="match_parent" 67 android:layout_height="50dp" 68 android:layout_marginLeft="10dp" 69 android:gravity="center_vertical" 70 android:textColor="@android:color/white" /> 71 72 <LinearLayout 73 android:layout_width="match_parent" 74 android:layout_height="wrap_content" 75 android:gravity="center_vertical" 76 77 android:orientation="horizontal" > 78 79 <LinearLayout 80 android:layout_width="fill_parent" 81 android:layout_height="match_parent" 82 android:layout_weight="1" 83 android:gravity="center_vertical" 84 android:orientation="horizontal" 85 android:padding="10dp" > 86 87 <ImageView 88 android:layout_width="wrap_content" 89 android:layout_height="wrap_content" 90 android:src="@drawable/fax_icon" /> 91 92 <TextView 93 android:id="@+id/tv_fax" 94 android:layout_width="100dp" 95 android:layout_height="wrap_content" 96 android:layout_marginLeft="10dp" 97 android:textColor="@android:color/white" /> 98 </LinearLayout> 99 100 <LinearLayout 101 android:id="@+id/tel_layout" 102 android:layout_width="fill_parent" 103 android:layout_height="match_parent" 104 android:layout_weight="1" 105 android:background="@drawable/station_tel_selected" 106 android:gravity="center_vertical" 107 android:orientation="horizontal" 108 android:padding="10dp" > 109 110 <ImageView 111 android:layout_width="wrap_content" 112 android:layout_height="wrap_content" 113 android:src="@drawable/tel_icon" /> 114 115 <TextView 116 android:id="@+id/tv_tel" 117 android:layout_width="100dp" 118 android:layout_height="wrap_content" 119 android:layout_marginLeft="10dp" 120 android:textColor="@android:color/white" /> 121 </LinearLayout> 122 </LinearLayout> 123 124 <LinearLayout 125 android:layout_width="match_parent" 126 android:layout_height="0.5dp" 127 android:background="@android:color/black" > 128 </LinearLayout> 129 130 </LinearLayout>"@drawable/tel_icon" /> 131 132 <TextView 133 android:id="@+id/tv_tel" 134 android:layout_width="100dp" 135 android:layout_height="wrap_content" 136 android:layout_marginLeft="10dp" 137 android:textColor="@android:color/white" /> 138 </LinearLayout> 139 </LinearLayout> 140 141 <LinearLayout 142 android:layout_width="match_parent" 143 android:layout_height="0.5dp" 144 android:background="@android:color/black" > 145 </LinearLayout> 146 147 </LinearLayout>
标签:des android style blog http io color ar os
原文地址:http://www.cnblogs.com/ylt-niuniu/p/4079265.html