天天看點

ExpandableListView動态編輯 item出現convertView 複用錯亂問題

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;
       	 }
    	});

}      
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;
}      
}

繼續閱讀