读书人

BaseAdapter中getView中处置点击事件

发布时间: 2012-09-19 13:43:54 作者: rapoo

BaseAdapter中getView中处理点击事件

static final class MyAdapter extends BaseAdapter {?
?
? ? /** override other methods here */?
?
? ? @Override?
? ? public View getView(final int position, View convertView, ViewGroup parent) {?
? ? ? ? ViewHolder holder;?
?
? ? ? ? if (convertView == null) {?
? ? ? ? ? ? // inflate the view for row from xml file?
?
? ? ? ? ? ? // keep a reference to each widget on the row.?
? ? ? ? ? ? // here I only care about the button?
? ? ? ? ? ? holder = new ViewHolder();?
? ? ? ? ? ? holder.mButton = (Button)convertView.findViewById(R.id.button);?
? ? ? ? ? ? convertView.setTag(holder);?
? ? ? ? } else {?
? ? ? ? ? ? holder = (ViewHolder)convertView.getTag();?
? ? ? ? }?
?
? ? ? ? // redefine the action for the button corresponding to the row?
? ? ? ? holder.mButton.setOnClickListener(new OnClickListener() {?
? ? ? ? ? ? @Override?
? ? ? ? ? ? public void onClick(View v) {?
? ? ? ? ? ? ? ? // do something depending on position?
? ? ? ? ? ? ? ? performSomeAction(position);?
? ? ? ? ? ? ? ? // mark data as changed?
? ? ? ? ? ? ? ? MyAdapter.this.notifyDatasetChanged();?
? ? ? ? ? ? }?
? ? ? ? }?
? ? }?
? ? static final class ViewHolder {?
? ? ? ? // references to widgets?
? ? ? ? Button mButton;?
? ? }?
}?

读书人网 >移动开发

热点推荐