刚在项目中做完了一个ExpandableListView的模块,GroupView和childView都是动态获取的,先上一下效果图看一下
上面是大项,下面是小项
1.去掉左边的小箭头,设置下面这个属性即可
lvInsuranceTime.setGroupIndicator(null);
2.设置childView可点击,就是两全险,终身寿险可点击,只要将BaseExpanableAdapter中重写的isChildeSelectable这个返回值改为true即可
@Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; }
3.设置group不可点击,也就是上面的大项,不可点击,只需要重写下面的方法,返回true即可
mAdapter = new InsuranceCompanyTypeAdapter(this, OutSizeDatas); lvInsuranceTime.setAdapter(mAdapter); //首次加载全部展开 for (int i = 0; i < OutSizeDatas.size(); i++) { lvInsuranceTime.expandGroup(i); } //让其group不能被点击 lvInsuranceTime.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { return true; } });
当然这个方法,还有一个setOnChildClickListener这个方法,是设置点击child的时候生效,但是切记,一定要将BaseExpanableAdapter中重写的isChildeSelectable这个返回值改为true,否则child的点击事件,是不会生效的
4.下面将Adapter中实现的方法贴出来,有详细的注释
public class InsuranceCompanyTypeAdapter extends BaseExpandableListAdapter{ private ArrayList<InsuraceCompanyTypeBean.DataEntity> datas; private Context mContext; // private ArrayList<InsuraceCompanyTypeBean.DataEntity.SubDictionaryListEntity> bean; // private final int size; private static final int TYPE_ITEM = 0; private static final int TYPE_SEPARATOR = 1; private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1; public InsuranceCompanyTypeAdapter(Context context,ArrayList<InsuraceCompanyTypeBean.DataEntity> datas) { this.datas = datas; this.mContext = context; // this.bean = bean; // size = datas.size() * bean.size(); } //这里是获取group中的数量,也就是寿险一类的数量 @Override public int getGroupCount() { return datas.size(); } //这里是获取每个group中child的数量 @Override public int getChildrenCount(int groupPosition) { return datas.get(groupPosition).subDictionaryList.size(); } //这里是获取组的当前的item @Override public Object getGroup(int groupPosition) { return datas.get(groupPosition); } //获取当前组中的child中的item @Override public Object getChild(int groupPosition, int childPosition) { return datas.get(groupPosition).subDictionaryList.get(childPosition); } //这里一般都是返回这个,不用管 @Override public long getGroupId(int groupPosition) { return groupPosition; } //同上 @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } //同上 @Override public boolean hasStableIds() { return false; } //注意,这里就是相当于listView中的getView,只不过有两个,一个是group的,一个是child的,这里就不详细说了,会listView,就肯定会这个 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewHolder holder = null; InsuraceCompanyTypeBean.DataEntity dataEntity = datas.get(groupPosition); if(convertView == null){ holder = new ViewHolder(); convertView = View.inflate(mContext,R.layout.insurace_type_item,null); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.tvGroupName = (TextView) convertView.findViewById(R.id.tv_catalog); holder.tvGroupName.setText(dataEntity.kindText); return convertView; } //这里是childView,同上 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ViewHolder holder = null; InsuraceCompanyTypeBean.DataEntity.SubDictionaryListEntity subDictionaryListEntity = datas.get(groupPosition).subDictionaryList.get(childPosition); if(convertView == null){ holder = new ViewHolder(); convertView = View.inflate(mContext,R.layout.insurace_type_item2,null); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.tvChildName = (TextView) convertView.findViewById(R.id.tv_insurance_company_item_name2); holder.ivChoose = (ImageView) convertView.findViewById(R.id.iv_term_insurance_choosed2); holder.tvChildName.setText(subDictionaryListEntity.itemText); if(datas.get(groupPosition).subDictionaryList.get(childPosition).isChoose){ holder.ivChoose.setVisibility(View.VISIBLE); }else{ holder.ivChoose.setVisibility(View.INVISIBLE); } return convertView; } //这里是设置childItem是否可被点击,true就是可点击,false不可点击 @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } static class ViewHolder{ TextView tvGroupName; TextView tvChildName; ImageView ivChoose; }
好了,基本这样就实现了上面的效果了,刚开始也是搞了很久,不过搞定了,哈哈哈哈~~~