关闭

GridView实现多选

标签: GridView多选android
203人阅读 评论(0) 收藏 举报
分类:

这是GridView实现多选的小demo,我自己的项目中取出来的,群上面有人在要,所以写一下,直接代码,代码里面有注释,这里不多说,先看效果图,再看代码


public class GvselectClassAdapter extends BaseAdapter{

	private android.widget.AbsListView.LayoutParams layoutParams;
	private List<Itembean> lists;
	
	private Context context = null;
	/**
	 * CheckBox 是否选择的存储集合,key 是 position , value 是该position是否选中
	 */
	private Map<Integer, Boolean> isCheckMap = new HashMap<Integer, Boolean>();
	public GvselectClassAdapter(Context context,List<Itembean> list) {
		this.context = context;
		this.lists = list;
		// 初始化,默认都没有选中
		configCheckMap(false);
		DisplayMetrics dm = new DisplayMetrics();
		((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
		layoutParams = new GridView.LayoutParams(
				dm.widthPixels / 4,
				dm.widthPixels/ 8);
	}
	/**
	 * 首先,默认情况下,所有项目都是没有选中的.这里进行初始化
	 */
	public void configCheckMap(boolean bool) {

		for (int i = 0; i < lists.size(); i++) {
			isCheckMap.put(i, bool);
		}

	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lists == null ? 0 : lists.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return lists.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public View getView(final int position, View v, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewGroup layout = null;
		if (v == null) {
			layout = (ViewGroup) LayoutInflater.from(context).inflate(
					R.layout.gridview_items, parent, false);
			layout.setLayoutParams(layoutParams);
		
		}else{
			layout = (ViewGroup) v;
		}
		/*
		 * 获得该item 是否允许删除
		 */
		Itembean item = lists.get(position);
		boolean canRemove = item.isCanRemove();
		
		CheckBox cbCheck = (CheckBox) layout.findViewById(R.id.tv_ss1);
		cbCheck.setText(item.getTv());
		cbCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				/*
				 * 将选择项加载到map里面寄存
				 */
				isCheckMap.put(position, isChecked);
			}
		});
		if (!canRemove) {
			cbCheck.setChecked(false);
		} else {
			if (isCheckMap.get(position) == null) {
				isCheckMap.put(position, false);
			}

			cbCheck.setChecked(isCheckMap.get(position));
			ViewHolder holder = new ViewHolder();
			holder.cbCheck = cbCheck;
			/**
			 * 将数据保存到tag
			 */
			layout.setTag(holder);
		}
		return layout;
	}

	/**
	 * 增加一项的时候
	 */
	public void add(Itembean bean) {
		this.lists.add(0, bean);

		// 让所有项目都为不选择
		configCheckMap(false);
	}

	// 移除一个项目的时候
	public void remove(int position) {
		this.lists.remove(position);
	}
	
//获取选择的集合
	public Map<Integer, Boolean> getCheckMap() {
		return this.isCheckMap;
	}

	public static class ViewHolder {

		public CheckBox cbCheck = null;
		public TextView title = null;
		public Object data = null;

	}

	public List<Itembean> getDatas() {
		return lists;
	}


再看Activity

public class TestSetLabelFragment extends Fragment implements OnClickListener {
	private TextView tv_navigtion_title,tv_register;
	private List<Itembean> list = new ArrayList<Itembean>();
	private List<String> listsNo = new ArrayList<String>();
	private GvselectClassAdapter Gadapter;
	private GridView gv_list;
	private String[] string = new String[]{ "创业", "科技", "课程", "生活", "运动", "商务", "公益", "社交",
			"亲子", "电影", "娱乐", "音乐", "校园", "文化", "其它" };
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setdatas();
	}
	// 模拟假数据
	private void setdatas() {
		// TODO Auto-generated method stub
		for (int i = 0; i < string.length; i++) {
			Itembean itembean = new Itembean(string[i], false);
			list.add(itembean);
		}
		

	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_select_class, container,
				false);
		tv_navigtion_title = (TextView) view
				.findViewById(R.id.tv_navigtion_title);
		tv_register = (TextView) view
				.findViewById(R.id.tv_register);
		tv_navigtion_title.setText("多选demo");
		gv_list = (GridView) view.findViewById(R.id.gv_list);
		gv_list.setColumnWidth(10);
		gv_list.setVerticalSpacing(15);
		tv_register.setOnClickListener(this);
		Gadapter = new GvselectClassAdapter(getActivity(), list);
		gv_list.setAdapter(Gadapter);
		
		gv_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				if (arg1.getTag() instanceof ViewHolder) {

					ViewHolder holder = (ViewHolder) arg1.getTag();

					// 会自动出发CheckBox的checked事件
					holder.cbCheck.toggle();

				}
			}
		});
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_register:
		   String datas = "";
			Map<Integer, Boolean> map = Gadapter.getCheckMap();
			// 进行遍历
		   int j=0;
			for (int i = 0; i < list.size(); i++) {
				if (map.get(i) != null && map.get(i)) {
					if(map.get(i)){//如果为true
						j +=1;//记录是否有选择的
						Itembean itembean = list.get(i);//取出选择的数据
						datas  += itembean.getTv()+",";//为了显示做了拼接展示
					}
				}
			}
			if(j==0){
				Toast.makeText(getActivity(), "请选择", 0).show();
				return;
			}
			Toast.makeText(getActivity(), datas.substring(0,datas.length()-1), 0).show();
				Log.d("tag", "总数"+map.size()+datas.substring(0,datas.length()-1));
			break;

		default:
			break;
		}
	}



感兴趣的可以下载来看看,不用积分..

下载地址:http://download.csdn.net/detail/lin708911156/9276933


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3349次
    • 积分:65
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    文章存档