观看此Google I / O视频后The world of ListView我了解getView()
被多次调用。我想像这样在OnClickLister
内设置一个getView()
。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//code goes here
}
});
我想知道是否有更好的方法来设置OnClickLister
中的getView()
?
答案 0 :(得分:0)
我使用这样的东西:
public class CustomBaseAdapter extends BaseAdapter {
...
private OnClickListener mButtonClickListener;
static class ViewHolder {
...
public Button buttonHolder;
}
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
holder.buttonHolder.setOnClickListener(this.mButtonClickListener);
return convertView;
}
public void setButtonClickListener(OnClickListener onClickListener) {
this.mButtonClickListener = onClickListener;
}
}
然后,在Activity / Fragment中设置如下:
adapter = new CustomBaseAdapter(...);
adapter.setButtonClickListener(mButtonListener);
private OnClickListener mButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
...
}
};
答案 1 :(得分:-1)
别。 你这样做:
ListView listView = (ListView) findViewById(R.id.lv_whatever_name);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
//TODO do whatever with your item
}
});