我将Facebook原生广告整合到GridView中。目前,我展示了测试广告。除播放视频的播放器外,它的工作正常。
如果用户没有与GridView进行交互,则MediaView播放视频就好了。 滚动GridView时,当广告重新出现在屏幕上时,视频会暂停并恢复。
向上和向下滚动网格几次后, MediaView不再显示视频,只显示一个灰色矩形。
出于好奇,当MediaView为灰色时,我试图在我的设备上运行Ui Automatic Viewer。我注意到一些有趣的东西,但我真的无法理解。
在View层次结构中,我可以看到带有一些子FrameLayout的GridView(适配器给出的Views的容器)。这包括原生广告和其他观点。
但当MediaView为灰色时,其FrameLayout 不会显示在视图层次结构中!但它在屏幕上呈现得很好!
我对自己的看法感到非常困惑。
此外,当我将这些广告整合到RecyclerView中时,我没有遇到这个问题(或者至少没有注意到它)。
让我们谈谈代码。我有一个引用指向Facebook原生广告视图。
建议欢迎:)
以下是为GridView提供视图的适配器的代码:
public class AdapterGridGallery extends BaseAdapter implements AdListener {
private static int POSITION_AD = 4;
private List<QuizzModel> listQuizzes;
int heightViews;
FragmentGallery fragmentGallery;
View facebookAdView;
private NativeAd facebookNativeAd;
private boolean nativeAdSet = false;
public AdapterGridGallery(FragmentGallery fragment, int height) {
heightViews = height;
fragmentGallery = fragment;
facebookNativeAd = new NativeAd(fragment.getContext(), "my_tag");
facebookNativeAd.setAdListener(this);
facebookNativeAd.loadAd();
}
public void updateData(List<QuizzModel> list) {
listQuizzes = list;
notifyDataSetChanged();
}
@Override
public int getCount() {
return listQuizzes != null ? listQuizzes.size() + 1 : 0;
}
@Override
public Object getItem(int i) {
return listQuizzes.get(i);
}
@Override
public long getItemId(int i) {
return listQuizzes.get(i).getId();
}
@Override
public int getItemViewType(int position) {
if (position == POSITION_AD)
return 0;
else
return 1;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View viewQuizz = null;
switch (getItemViewType(position)) {
case 0:
if (facebookAdView == null) {
facebookAdView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.view_facebook_native, viewGroup, false);
//Settings the height of the view
AbsListView.LayoutParams params = (AbsListView.LayoutParams) facebookAdView.getLayoutParams();
params.height = heightViews;
params.width = AbsListView.LayoutParams.MATCH_PARENT;
facebookAdView.setLayoutParams(params);
}
viewQuizz = facebookAdView;
viewQuizz.setTag(0);
if (facebookNativeAd.isAdLoaded()) {
if (!nativeAdSet) {
Log.d("NativeAdList", "update views resources");
nativeAdSet = true;
ImageView nativeAdIcon = (ImageView) facebookAdView.findViewById(R.id.native_ad_icon);
TextView nativeAdTitle = (TextView) facebookAdView.findViewById(R.id.native_ad_title);
TextView nativeAdBody = (TextView) facebookAdView.findViewById(R.id.native_ad_body);
MediaView nativeAdMedia = (MediaView) facebookAdView.findViewById(R.id.native_ad_media);
TextView nativeAdSocialContext = (TextView) facebookAdView.findViewById(R.id.native_ad_social_context);
Button nativeAdCallToAction = (Button) facebookAdView.findViewById(R.id.native_ad_call_to_action);
nativeAdSocialContext.setText(facebookNativeAd.getAdSocialContext());
nativeAdCallToAction.setText(facebookNativeAd.getAdCallToAction());
nativeAdTitle.setText(facebookNativeAd.getAdTitle());
nativeAdBody.setText(facebookNativeAd.getAdBody());
// Downloading and setting the ad icon.
NativeAd.Image adIcon = facebookNativeAd.getAdIcon();
NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);
// Download and setting the cover image.
nativeAdMedia.setNativeAd(facebookNativeAd);
nativeAdMedia.setAutoplay(true);
facebookNativeAd.registerViewForInteraction(facebookAdView);
nativeAdCallToAction.setVisibility(View.VISIBLE);
} else {
Log.d("NativeAdList", "views resources already set");
}
} else {
Log.d("NativeAdList", "nativeAdCallToAction is set invisible");
nativeAdCallToAction.setVisibility(View.INVISIBLE);
}
break;
case 1:
view = new CustomView();
}
return view;
}
@Override
public void onError(Ad ad, AdError adError) {
}
@Override
public void onAdLoaded(Ad ad) {
notifyDataSetChanged();
}
@Override
public void onAdClicked(Ad ad) {
}
}
以下是Ui Automator Viewer的截图。
答案 0 :(得分:3)
正如你所说
当我将这些广告整合到RecyclerView中时,我没有这个 问题(或者至少没有注意到它)。
我认为回收站视图对您来说非常合适。然后,不要尝试在gridview中重做同样的事情,只需使用LayoutManager
将回收者视图转换为grid or list。