ExpandableListView动态编辑 item出现convertView 复用错乱问题

本文探讨了在使用ExpandableListView时遇到的convertView复用问题,特别是在动态编辑item时导致显示错乱的常见情况。通过深入理解ListView的工作原理,结合具体的实例分析,提出了解决这类问题的策略和最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Activity 页面

	private int[][] tags = null;
	private int a;
	private int b;

	protected void init() {
    		setContentView(R.layout.activity_tank_list_editor);
    		Intent intent = getIntent();
    		mGroup = intent.getStringArrayListExtra("groupData");
    		Bundle bundle = intent.getExtras();
    		SerializableMap mMap = (SerializableMap) bundle.get("childData");
    		mChild = mMap.getMap();
	//这里a,b分别对应的是ExpandableListView父类和子类的个数,
    		a=50;
   		b=100;

    		tags = new int[a][b];

    		findView();
    		setView();
    		setListener();
	}
	private void setListener() {
   	//这里我们要给点击过的item 设置状态,

    	   expandListView.setOnChildClickListener(new ExpandableListView.
			OnChildClickListener() {
       		 @Override
        	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, 
		int childPosition, long id) {

            	ExpandableListViewTankEditor.GroupChildHolder holder = (ExpandableListViewTankEd				itor.GroupChildHolder) v.getTag();

               //此处按钮可以用自定义checkBox,但是好像得解决抢占焦点的问题

            	if (holder.title.getTextColors().getDefaultColor() == getResources().getColor
			(R.colo	r.grayColor)) {
                holder.title.setTextColor(getResources().getColor(R.color.icon_color));

                holder.ll_icon.setBackground(getResources().getDrawable
			(R.drawable.icon_background_circular));

                holder.iv_select.setImageResource(R.drawable.select);

                tags[groupPosition][childPosition] = 1;


            } else {
                holder.title.setTextColor(getResources().getColor(R.color.grayColor));

                holder.ll_icon.setBackground(getResources().getDrawable
			(R.drawable.icon_background_circular_gray));

                holder.iv_select.setImageResource(R.drawable.no_select);

                tags[groupPosition][childPosition] = 0;
            }

            edAdapter.setTags(tags);
            //edAdapter.notifyDataSetChanged();


            return false;
       	 }
    	});

}
   adapter class:
	public class ExpandableListViewTankEditor extends BaseExpandableListAdapter {
		private List<String> mGroup;
		private Map<String, List<String>> mChild;
		private Context context;

		private int[][] tags = null;


		public void setTags(int[][] tags) {
    			this.tags = tags;
		}


		public ExpandableListViewTankEditor(Context context, List<String> group, 
			Map<String, List<String>> child,int[][] tags) {
    			super();
    			this.context = context;
    			this.mGroup = group;
    			this.mChild = child;
    			this.tags=tags;

    			for (int i = 0; i < 50; i++) {
        		    for (int j = 0; j < 50; j++) {
            				tags[i][j] = 0;
        		    }
    			}


		}
		public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    			GroupChildHolder holder;


    			if (convertView == null) {

       			convertView = UIUtils.inflate(R.layout.item_tank_editor_adapter_child);

        		holder = new GroupChildHolder();

        		holder.icon = (ImageView) convertView.findViewById(R.id.iv_child_icon);
        		holder.title = (TextView) convertView.findViewById(R.id.tv_child_title);
        		holder.desc = (TextView) convertView.findViewById(R.id.tv_child_desc);
        		holder.select = (LinearLayout) convertView.findViewById
					(R.id.editor_select);
        		holder.iv_select = (ImageView) convertView.findViewById(R.id.iv_select);
        		holder.ll_icon = (LinearLayout) convertView.findViewById
					(R.id.line1_icon);


        		convertView.setTag(holder);
    			} else {
        		holder = (GroupChildHolder) convertView.getTag();

    		}

    		holder.title.setText(mChild.get(mGroup.get(groupPosition)).get(childPosition));

    		if (tags[groupPosition][childPosition] == 1) {
        		holder.title.setTextColor(context.getResources().getColor
				(R.color.icon_color));
        		holder.ll_icon.setBackground(context.getResources().getDrawable
				(R.drawable.icon_background_circular));
        		holder.iv_select.setImageResource(R.drawable.select);
    		} else {
        		holder.title.setTextColor(context.getResources().getColor
				(R.color.grayColor));
        		holder.ll_icon.setBackground(context.getResources().getDrawable
				(R.drawable.icon_background_circular_gray));
        		holder.iv_select.setImageResource(R.drawable.no_select);
    	}

    		return convertView;
	}
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
   	  //默认是false,此处设为true
    		return true;
}
}
 图片的话是效果图,大家可以参考一下,希望对大家有帮助,此文借鉴了别的博主的一些思路,在此非常感谢!
   
               
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值