ListView Item 选择隐藏

大家先看下效果:

ListView Item 选择隐藏_第1张图片

功能:

点击编辑开始出现左边按钮,点击左边按钮出现隐藏删除按钮,点击删除,删除本选项.


实现这个需要知道ListVeiw内部按钮监听方法:

在ListVeiw布局中加入:

android:descendantFocusability= "blocksDescendants"

我用一个int值记录当前选中项,int值主要是为了大家用Holder时防止出现选择超屏幕复用的情况,一个boolean记录当前的选中状态,代码不多:

@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.collectact_item, null);
			view = new ViewHolder();
			view.compile = (ImageButton) convertView.findViewById(R.id.btn_compile);
			view.delete = (ImageButton) convertView.findViewById(R.id.btn_delete);
			view.name = (TextView) convertView.findViewById(R.id.tv_name);
			convertView.setTag(view);
		} else {
			view = (ViewHolder) convertView.getTag();
		}
		if (!isEdit) {
			view.compile.setBackgroundResource(R.drawable.icon15);
			view.compile.setVisibility(View.GONE);
			view.delete.setVisibility(View.GONE);
		} else {
			view.compile.setVisibility(View.VISIBLE);
		}
		if (ids == position) {
			if (!os) {
				view.compile.setBackgroundResource(R.drawable.icon14);
				view.delete.setVisibility(View.VISIBLE);
			} else {
				view.compile.setBackgroundResource(R.drawable.icon15);
				view.delete.setVisibility(View.GONE);
			}
		} else {
			view.compile.setBackgroundResource(R.drawable.icon15);
			view.delete.setVisibility(View.GONE);
		}
		final int id = position;
		view.compile.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (-1 != ids) {
					if (ids == id) {
						System.out.println("相同");
						if (!os) {
							os = true;
						} else {
							os = false;
						}
					} else {
						os = false;
						ids = id;
					}
				} else {
					ids = id;
					os = false;
				}
				notifyDataSetChanged();
			}
		});
		final int index = position;
		view.delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				View vie = (View) v.getParent();
				ViewHolder holder = (ViewHolder) vie.getTag();
				holder.compile.setBackgroundResource(R.drawable.icon15);
				holder.delete.setVisibility(View.GONE);
				ids = -1;
				list.remove(index);
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

点击删除,删除当前item.

源码已经上传:

http://download.csdn.net/detail/wang_yubin/4681117


你可能感兴趣的:(ListView Item 选择隐藏)