Expandlistview的使用

首先expandlistview和listview的 大致用法是一模一样的

expandablelistview = (ExpandableListView) resutlview
.findViewById(R.id.expandlistview);
final SortExpandableAdapter sortExpandableAdapter = new SortExpandableAdapter(
this, profather);
expandablelistview.setAdapter(sortExpandableAdapter);
expandablelistview.setGroupIndicator(null);//去掉自带的箭头

//这个事多级菜单只展开其中的一个条目
expandablelistview.setOnGroupExpandListener(new OnGroupExpandListener() {  
   @Override  
   public void onGroupExpand(int groupPosition) {  
       for (int i = 0; i < sortExpandableAdapter.getGroupCount(); i++) {  
           if (groupPosition != i) {  
               expandablelistview.collapseGroup(i);  
           }  
       }  
   }  
});  

主要是介绍expandlistview的adpater是如何填充数据的

public class SortExpandableAdapter extends BaseExpandableListAdapter implements
OnClickListener {
public Context context;
public ArrayList<ProductSortAll> profather;
public ListItemparent listparent;
public ListItemchild listchild;


static class ListItemparent {
public ImageView parentpic;
public ImageView arrow;
public TextView sortName;
public TextView sortDescritpion;


}


static class ListItemchild {
public TextView childsortName;
}


public SortExpandableAdapter(MoreSortNewActivity moreSortNewActivity,
ArrayList<ProductSortAll> profather) {
this.context = moreSortNewActivity;
this.profather = profather;


}


@Override
public int getGroupCount() {
return profather.size();// 一级分类的数量
}


@Override
public int getChildrenCount(int groupPosition) {


return profather.get(groupPosition).getTypeArray().size() + 1;// 二级分类的数量
}


@Override
public Object getGroup(int groupPosition) {


return profather.get(groupPosition);
}


@Override
public Object getChild(int groupPosition, int childPosition) {
if (childPosition == 0) {
return profather.get(groupPosition);
} else {
return profather.get(groupPosition).getTypeArray()
.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;
}


@SuppressLint("NewApi")
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {


if (convertView == null) {
convertView = View.inflate(context, R.layout.view_parentsort, null);
listparent = new ListItemparent();
listparent.parentpic = (ImageView) convertView
.findViewById(R.id.pic);
listparent.sortName = (TextView) convertView
.findViewById(R.id.parentsortname);
listparent.sortDescritpion = (TextView) convertView
.findViewById(R.id.sortdescription);
listparent.arrow = (ImageView) convertView.findViewById(R.id.arrow);
convertView.setTag(listparent);
} else {
listparent = (ListItemparent) convertView.getTag();
}


ImageLoader.getInstance().displayImage(
profather.get(groupPosition).getPhotoUrl(),
listparent.parentpic, BitmapUtil.getPersonLoading());
listparent.sortName.setText(profather.get(groupPosition)
.getProductSortName());
String childitemdescription = getStringchilditems(profather.get(
groupPosition).getTypeArray());
listparent.sortDescritpion.setText(childitemdescription);
if (isExpanded) {
listparent.arrow.setImageResource(R.drawable.downarrowpic);
} else {
listparent.arrow.setImageResource(R.drawable.uparrowpic);
}


return convertView;
}


private String getStringchilditems(List<ProductSortSon> typeArray) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < typeArray.size(); i++) {
result.append(typeArray.get(i).getName()+".");
}

if( result.length()>0){
return result.substring(0, result.length() - 1).toString();
}else{
return "";
}

}


@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(context, R.layout.view_childsort, null);
listchild = new ListItemchild();
listchild.childsortName = (TextView) convertView
.findViewById(R.id.childsortname);
convertView.setTag(listchild);
} else {
listchild = (ListItemchild) convertView.getTag();


}
if (childPosition == 0) {
listchild.childsortName.setText(profather.get(groupPosition)
.getProductSortName());
} else {
listchild.childsortName.setText(profather.get(groupPosition)
.getTypeArray().get(childPosition - 1).getName());
}
convertView.setTag(R.id.tag_first, groupPosition);
convertView.setTag(R.id.tag_second, childPosition);
convertView.setOnClickListener(this);
return convertView;
}


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


@Override
public void onClick(View v) {
Integer childPosition = (Integer) v.getTag(R.id.tag_second);
Integer parentPosition = (Integer) v.getTag(R.id.tag_first);
Intent intent = new Intent(context, ServiceForClassfyActivity.class);
if (childPosition == 0) {
intent.putExtra("DataType", "2");
intent.putExtra("productSortId", profather.get(parentPosition)
.getProductSortId());
intent.putExtra("productSortName", profather.get(parentPosition)
.getProductSortName());
intent.putExtra("sort", 0);
context.startActivity(intent);
} else {
intent.putExtra("DataType", "2");
intent.putExtra("productSortId", profather.get(parentPosition)
.getTypeArray().get(childPosition - 1).getValue());
intent.putExtra("productSortName", profather.get(parentPosition)
.getTypeArray().get(childPosition - 1).getName());
intent.putExtra("sort", 0);


context.startActivity(intent);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值