我已成功设法扩展CursorAdapter并更改bindView-和newView-methods,以便能够在列表中包含可点击按钮。按钮(实际上是一个可点击的ImageView)使用一个选择器作为backgroundresource,一切正常,只有当我点击list-item-view时,按钮/ imageview的选择器被激活,因此看起来好像我点击了按钮/图像视图,即使我没有。 onclicklistener中绑定到button / imageview的方法虽然在单击list-item-view时未激活,因此正常工作,它只是选择器。这是我的CustomCursorAdapter:
public class CustomCursorAdapter extends CursorAdapter {
Context context;
LayoutInflater inflater;
AlertDialog.Builder builder;
public CustomCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context c, Cursor cursor){
ViewHolder holder = (ViewHolder) view.getTag();
final Context context = c;
final String name = cursor.getString(cursor.getColumnIndex("name"));
final String number = cursor.getString(cursor.getColumnIndex("number"));
final String mail = cursor.getString(cursor.getColumnIndex("mail"));
holder.getTvName().setText(name);
holder.getTvNumber().setText(number);
holder.getTvMail().setText(mail);
int row_id = cursor.getInt(cursor.getColumnIndex("_id"));
holder.getCallButton().setTag(row_id);
holder.getCallButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder = new AlertDialog.Builder(context);
builder.setMessage("Call " + name + "?")
.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String uri = "tel:" + nummer.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
context.startActivity(intent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.create().show();
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View convertView = inflater.inflate(R.layout.contact_list_row, parent, false);
ViewHolder holder = new ViewHolder();
holder.setCallButton((ImageView) convertView.findViewById(R.id.bCall));
holder.setTvName((TextView) convertView.findViewById(R.id.tvName));
holder.setTvNumber((TextView) convertView.findViewById(R.id.tvNumber));
holder.setTvMail((TextView) convertView.findViewById(R.id.tvMail));
convertView.setTag(holder);
return convertView;
}
public static class ViewHolder {
private TextView tvName, tvNumber, tvMail;
private ImageView bCall;
public ImageView getCallButton() {
return bCall;
}
public void setCallButton(ImageView bCall) {
this.bCall = bCall;
}
public TextView getTvName() {
return tvName;
}
public void setTvName(TextView tvName) {
this.tvName = tvName;
}
public TextView getTvNumber() {
return tvNumber;
}
public void setTvNumber(TextView tvNumber) {
this.tvNumber = tvNumber;
}
public TextView getTvMail() {
return tvMail;
}
public void setTvMail(TextView tvMail) {
this.tvMail = tvMail;
}
}
}
这是listView-XML:
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent" />
和contact_list_row.XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_background"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="@+id/bCall"
android:background="@drawable/button"
android:clickable="true"
android:src="@drawable/ic_clean_phone" />
选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/h_main_button_background_pressed"></item>
<item android:drawable="@drawable/h_main_button_background"></item>
</selector>
如果您发现任何其他错误或不必要的代码,我也非常感谢!