1.
在getView方法中写入:
//记录当前展开项的索引
private int expandPosition = -1;
QueryOrderResultBean lvItemBean = dataList.get(position);
if (null != lvItemBean) {
viewHolder.all_layout.setOnClickListener(new OnLvItemClickListener(position));
//如果点击的是当前项,则将其展开,否则将其隐藏
if(expandPosition == position){
viewHolder.order_detials.setVisibility(View.VISIBLE);
viewHolder.order_img.setVisibility(View.VISIBLE);
}else{
viewHolder.order_detials.setVisibility(View.GONE);
viewHolder.order_img.setVisibility(View.GONE);
}
}
2.某个控件的监听事件
class OnLvItemClickListener implements OnClickListener {
private int position;
public OnLvItemClickListener(int position) {
super();
this.position = position;
}
@Override
public void onClick(View v) {
//如果当前项为展开,则将其置为-1,目的是为了让其隐藏,如果当前项为隐藏,则将当前位置设置给全局变量,让其展开,这也就是借助于中间变量实现布局的展开与隐藏
if(expandPosition == position){
expandPosition = -1;
}else{
expandPosition = position;
}
notifyDataSetChanged();
}
}