BaseAdapter怎么添加点击事件
BaseAdapter怎么添加点击事件 请详细些 在那个里面添加点击事件呢
BaseAdapter怎么添加点击事件 请详细些 在那个里面添加点击事件呢
2016-08-16
static final class MyAdapter extends BaseAdapter {
@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;
}
}
在内部类中定义成员变量button,接着利用ViewHolder优化代码,创建与ConvertView之间的联系,利用单击事件setOnClickListener方法,就ok了
举报