android listview、GridView中item点击后改变其他item中的状态 setOnItemClickListener onItemClick

先看效果:

android listview、GridView中item点击后改变其他item中的状态 setOnItemClickListener onItemClick_第1张图片android listview、GridView中item点击后改变其他item中的状态 setOnItemClickListener onItemClick_第2张图片

item的xml文件如下:




    

背景颜色设置:

ware_house_filter_bg.xml




    
    
	
    
ware_house_filter_bg_selected



    
            

            

            
        

ware_house_filter_bg_unselect.xml




    
            

            

            
        

添加适配器:

item_area = new ArrayList<>();
			item_type = new ArrayList<>();

			for (int i = 0; i < 19; i++) {
				HashMap map = new HashMap<>();
				HashMap map2 = new HashMap<>();
				map.put("itemName", "区域 " + i);
				map2.put("itemName", "类型 " + i);
				item_area.add(map);
				item_type.add(map2);

			}

// 区域
			adapterArea = new SimpleAdapter(
					WareHouseFragment.this.getActivity(), item_area,
					R.layout.item_text_grid, new String[] { "itemName" },
					new int[] { R.id.item_grid_tvShow });
			gv_area.setAdapter(adapterArea);
增加点击事件:

private int indexArea = -1;

gv_area.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(android.widget.AdapterView parent,
					View view, int position, long id) {
				// TODO Auto-generated method stub

				if (indexArea != -1) {
					CheckedTextView tvShowClose = (CheckedTextView) parent
							.getChildAt(indexArea).findViewById(
									R.id.item_grid_tvShow);
					tvShowClose.setChecked(false);
				}
				indexArea = position;
				CheckedTextView tvShow = (CheckedTextView) view
						.findViewById(R.id.item_grid_tvShow);
				if (tvShow.isChecked()) {
					tvShow.setChecked(false);
				} else {
					tvShow.setChecked(true);
				}

			}
		});







你可能感兴趣的:(Android)