Android ListView item内点击事件复用
@Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; OnClick listener = null; if (convertView == null) { convertView = View.inflate(mContext, R.layout.listview_item, null); holder = new ViewHolder(); holder.no = (TextView) convertView.findViewById(R.id.no); holder.click = (TextView) convertView.findViewById(R.id.click); listener = new OnClick();//在这里新建监听对象 holder.click.setOnClickListener(listener); convertView.setTag(holder); convertView.setTag(holder.click.getId(), listener);//对监听对象保存 } else { holder = (ViewHolder) convertView.getTag(); listener = (OnClick) convertView.getTag(holder.click.getId());//重新获得监听对象 } String value = list.get(position);//设置监听对象的值 holder.no.setText(value); listener.setPosition(position); Log.d(TAG, "position is " + position + " listener is " + listener.toString()); return convertView; } class OnClick implements OnClickListener { int position; public void setPosition(int position) { this.position = position; } @Override public void onClick(View v) { Log.d(TAG, list.get(position)); } }
class ViewHolder {
TextView no;
TextView click; }