Android 在ArrayAdapter中以编程方式添加自定义视图(广告空间)
我试图以编程方式将自定义视图添加到自定义适配器中的图像列表视图中。目前我正在尝试将其添加为第三个“图像”。我的客户适配器不稳定,因此不确定如何使其工作。填充所有的图片效果很好,但无法在两者之间获得广告空间。有什么建议吗Android 在ArrayAdapter中以编程方式添加自定义视图(广告空间),android,android-arrayadapter,custom-adapter,Android,Android Arrayadapter,Custom Adapter,我试图以编程方式将自定义视图添加到自定义适配器中的图像列表视图中。目前我正在尝试将其添加为第三个“图像”。我的客户适配器不稳定,因此不确定如何使其工作。填充所有的图片效果很好,但无法在两者之间获得广告空间。有什么建议吗 @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; if (rowView == null)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.image_row, null);
ViewHolder viewHolder = new ViewHolder();
if(position==3) {
adSpace = new AdSpace(context, "c026dd7e-60a5-46c9-948f-38eb18a", true);
adSpace.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
Log.i("AdSpace","Adding adspace");
} else {
viewHolder.image = (ImageView) rowView.findViewById(R.id.offer_row);
}
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
imageLoader.displayImage(imageURLArray.get(position), holder.image, options);
return rowView;
}
static class ViewHolder {
public ImageView image;
public AdSpace adspace;
}
list.xml:
<?xml version="1.0" encoding="utf-8"?>
<"packagename".ZoomView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/offer_list" />
image_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/"packagename""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/offer_row"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:contentDescription="Image" />
您正在创建adSpace,但从未使用过它
if(position==3) {
adSpace = new AdSpace(context, "c026dd7e-60a5-46c9-948f-38eb18a", true);
adSpace.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
Log.i("AdSpace","Adding adspace");
return adSpace;
}
您可以从viewholder中删除adSpace。干杯,现在广告已挂起,但滚动时会崩溃。还从viewholder中删除了adspace。有什么想法吗?没关系,只是需要对if/else进行一些重构:)将if(position==3)条件保留在if(rowView==null)条件之外,因为当它尝试在位置3处循环视图时滚动,它将循环adView而不是适配器视图。