我想在 GridView 布局中显示 Native AdView。在适配器内部,我创建了两个不同的视图,它们扩展 ViewHolder 并返回特定位置的特定视图。代码如下:
OnCreateViewHolder()
//Inflate Custom Layout for View
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = null;
RecyclerView.ViewHolder vh = null;
if (viewType == AD_TYPE) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.video_list_ad_view_layout, parent, false);
vh = new VideoAdapter.AdViewHolder(itemView);
} else {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.video_card_layout, parent, false);
vh = new VideoAdapter.VideoViewHolder(itemView);
}
return vh;
}
AdView 持有者类:
//Separate AdViewHolder for displaying the Ads in the list
private static class AdViewHolder extends RecyclerView.ViewHolder {
AdView mAdView;
AdViewHolder(View view) {
super(view);
mAdView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(String.valueOf(R.string.test_device_id))
.build();
mAdView.loadAd(adRequest);
}
}
VideoViewHolder()
public class VideoViewHolder extends RecyclerView.ViewHolder {
//Code is here for custom videoViewHolder
}
此广告与其他网格项目一起显示在 GridView 布局内。我试图设置 AdView 的大小,但它并没有帮助我。
我需要一些解决方案,这样我才能在 GridLayout 中将 NativeAds 显示为 GridItem。
图片可在以下位置找到: Google Plus POST with Image Here