在listview的adapter中作item点击监听事件时会出现显示错乱的问题,这个问题的解决办法也就是为点击的控件加一个tag,以下代码:数组
public class PriceMenuDetailAdapter extends BaseAdapter {
private ArrayList list_quotation;
private Context context;
private boolean[] showControl;//代表对应的item是否须要展开
public PriceMenuDetailAdapter(ArrayList list_quotation, Context context) {
this.list_quotation = list_quotation;
this.context = context;
showControl = new boolean[list_quotation.size()];//此时数组中的默认值都是false
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.item_price_menu_detail, null);
viewHolder = new ViewHolder();
/*加载id*/
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
AddQuotationBean.ASQuotations asQuotations = list_quotation.get(i);
viewHolder.tv_space_name.setText(asQuotations.SpaceName);
viewHolder.tv_diff_sum.setText("¥" + Util.getNonScientificCount(asQuotations.SpaceTotalMoney + "", true));
viewHolder.ll_info.removeAllViews();
ArrayList list_detail = asQuotations.QDetails;
for (int k = 0; k < list_detail.size(); k++) {
viewHolder.ll_info.addView(returnView(list_detail.get(k), k, list_detail.size()));
}
viewHolder.price_detail_layout.setTag(i);//为点击的区域设置Tag为position
if (showControl[i]) {//加载item的时候查看是否须要打开或隐藏
viewHolder.ll_info.setVisibility(View.GONE);
} else {
viewHolder.ll_info.setVisibility(View.VISIBLE);
}
viewHolder.price_detail_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tag = (int) v.getTag();//获取点击位置对应的tag值
if (showControl[tag]) {//改变数组中对应的值
showControl[tag] = false;
} else {
showControl[tag] = true;
}
** notifyDataSetChanged();//从新加载**
}
});
return view;
}
private class ViewHolder{
private TextView tv_space_name;// 空间名
private TextView tv_diff_sum;// 差价小计
private LinearLayout ll_info;// 材料信息
private RelativeLayout price_detail_layout;//空间名和差价小计总体的布局
}
}
注意:咱们平时对 ListView 作的最多的操做就是 setOnItemClickListener,这个操做通常都是在 Activity 中进行的,此时响应区域是 Item 总体,无论你点击 Item 的哪一个角落都会响应。而对于每一个 Item 中子控件的事件监听(区别于整个Item,好比说 Item 中的按钮、输入框等等)都是在适配器类中添加,此时只有点击添加监听的子控件区域才会响应,至关于每一个 Item 中的该控件都添加了监听。OnClick 的响应优先级:子控件(元控件)> 父布局(可是不像 onTouch 事件有 Boolean 返回值那样,OnClick 事件是没有返回值的,便是“阻断式式响应”,不会再响应它所归属的上层控件)。ide
能够看到上面代码中加粗的" notifyDataSetChanged();//从新加载"这一句,这样写在adapter里面,若是是数据量大的状况下会形成listview滑动出现卡顿的现象,为了不出现这种状况,能够修改代码以下:布局
/**@title PriceMenuDetailAdapter 填写报价单详情页面ListView的适配器
* @author Eric
* @date 2017/3/30
**/
public class PriceMenuDetailAdapter extends BaseAdapter {
private ArrayList list_quotation;
private Context context;
private boolean[] isShowContentClose;//代表对应的item是否须要展开,true为隐藏,false为打开
public PriceMenuDetailAdapter(ArrayList list_quotation, Context context) {
this.list_quotation = list_quotation;
this.context = context;
isShowContentClose = new boolean[list_quotation.size()];//此时数组中的默认值都是false
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.item_price_menu_detail, null);
viewHolder = new ViewHolder();
/*加载id*/
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
AddQuotationBean.ASQuotations asQuotations = list_quotation.get(i);
viewHolder.tv_space_name.setText(asQuotations.SpaceName);
viewHolder.tv_diff_sum.setText("¥" + Util.getNonScientificCount(asQuotations.SpaceTotalMoney + "", true));
viewHolder.ll_info.removeAllViews();
ArrayList list_detail = asQuotations.QDetails;
for (int k = 0; k < list_detail.size(); k++) {
viewHolder.ll_info.addView(returnView(list_detail.get(k), k, list_detail.size()));
}
viewHolder.price_detail_layout.setTag(i);//为点击的区域设置Tag为position
//至关于初始化控件的显示或隐藏
if (isShowContentClose[i]) {//加载item的时候查看是否须要打开或隐藏
viewHolder.ll_info.setVisibility(View.GONE);
} else {
viewHolder.ll_info.setVisibility(View.VISIBLE);
}
actionClick(viewHolder.price_detail_layout, viewHolder.ll_info);
return view;
}
//点击事件监听
private void actionClick(final RelativeLayout price_detail_layout, final LinearLayout ll_info) {
try {
price_detail_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tag = (int) v.getTag();//获取点击位置对应的tag值
if (isShowContentClose[tag]) {//改变数组中对应的值
isShowContentClose[tag] = false;
} else {
isShowContentClose[tag] = true;
}
if (isShowContentClose[tag]) {//加载item的时候查看是否须要打开或隐藏
//手风琴效果,若是为true则隐藏若是为false则打开
ll_info.setVisibility(View.GONE);
} else {
ll_info.setVisibility(View.VISIBLE);
}
}
});
} catch (Exception e) {
LogUtil.e(getClass(), "actionClick()", e);
}
}
private class ViewHolder{
private TextView tv_space_name;// 空间名
private TextView tv_diff_sum;// 差价小计
private LinearLayout ll_info;// 材料信息
private RelativeLayout price_detail_layout;//空间名和差价小计总体的布局
}
}
总结:用一个集合加载position对应控件的tag状态,先初始化控件的隐藏或显示,在点击事件里面,改变对应集合的状态,而后改变控件的隐藏或显示.this