使用 ImageView 而不是 Button。按钮单击和 OnItemClickListener 不能一起工作。
这是一个工作示例:
MainActivity.java
public class MainActivity extends Activity {
private ListView customLV;
private CustomArrayAdapter adapter;
String strArray[] = { "First", "Second", "Third", "Forth", "Fifth" };
ArrayList<String> strAL = new ArrayList<String>(Arrays.asList(strArray));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customLV = (ListView) findViewById(R.id.customLV);
adapter = new CustomArrayAdapter(this, R.layout.list_item, strAL);
customLV.setAdapter(adapter);
customLV.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long arg3) {
Toast.makeText(getApplicationContext(), "List Item", Toast.LENGTH_SHORT)
.show();
}
});
}
}
CustomArrayAdapter.java
public class CustomArrayAdapter extends ArrayAdapter<String> {
Context context;
public CustomArrayAdapter(Context context, int textViewResourceId,
ArrayList<String> strAL) {
super(context, textViewResourceId, strAL);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.titleTV = (TextView) convertView
.findViewById(R.id.titleTV);
viewHolder.testBT = (ImageView) convertView.findViewById(R.id.testBT);
viewHolder.testBT.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Button Clicked",
Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.titleTV.setText(getItem(position));
return convertView;
}
class ViewHolder {
TextView titleTV;
ImageView testBT;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<TextView
android:id="@+id/titleTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<ImageView
android:id="@+id/testBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="onClick"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/customLV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>