請看看這個:OnItemClickListener。實施示例:setOnItemClickListener on custom ListView。
由於在列表視圖的每一行中都有3個按鈕,並且您有3個diff執行,所以還有另一種方法。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(null == convertView){
holder = new ViewHolder();
convertView = inflater.inflate(LAYOUT_RESOURCE, null);
holder.btn1 = (Button) convertView.findViewById(R.id.btn1);
holder.btn2 = (Button) convertView.findViewById(R.id.btn2);
holder.btn3 = (Button) convertView.findViewById(R.id.btn3);
//setting tag to reduce hierarchy lookup
convertView.setTag(holder);
} else {
//getting tag to obtain the already found views
holder = (ViewHolder) convertView.getTag();
}
//Updating fields
holder.btn1.setOnClickListener(commonListener);
holder.btn2.setOnClickListener(commonListener);
holder.btn3.setOnClickListener(commonListener);
//returning the populated view
return convertView;
}
OnClickListener commonListener = new OnClickListener(){
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
//do your work
break;
case R.id.btn2:
//do your work
break;
case R.id.btn3:
//do your work
break;
default:
break;
}
}
});
/**
* Holder class to improve performance. Helps in reducing the lookup for
* views.
*
*/
private static class ViewHolder {
Button btn1;
Button btn2;
Button btn3;
}
希望這會有所幫助。
你想要做什麼? –
每個單元格都有三個按鈕,我需要知道點擊了哪個確切的位置,因爲每個按鈕都會做一些獨立的操作。 –
然後你可以得到那個按鈕的ID。我認爲你不應該得到那個位置。 –