NativeAppInstallAdViewHolder(View view) { super(view); NativeAppInstallAdView adView = (NativeAppInstallAdView) view; // The MediaView will display a video asset if one is present in the ad, and the // first image asset otherwise. MediaView mediaView = (MediaView) adView.findViewById(R.id.appinstall_media); adView.setMediaView(mediaView); // Register the view used for each individual asset. adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline)); adView.setBodyView(adView.findViewById(R.id.appinstall_body)); adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action)); adView.setIconView(adView.findViewById(R.id.appinstall_app_icon)); adView.setPriceView(adView.findViewById(R.id.appinstall_price)); adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars)); adView.setStoreView(adView.findViewById(R.id.appinstall_store)); }
@Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { if (viewHolder==null) return; int itemViewType = viewHolder.getItemViewType(); if(itemViewType == getViewTypeAdInstall()) { NativeAppInstallAdView lvi1 = (NativeAppInstallAdView) viewHolder.itemView; NativeAppInstallAd ad1 = (NativeAppInstallAd) getItem(position); getInstallAdsLayoutContext().bind(lvi1, ad1); } else if(itemViewType == getViewTypeAdContent()) { NativeContentAdView lvi2 = (NativeContentAdView) viewHolder.itemView; NativeContentAd ad2 = (NativeContentAd) getItem(position); getContentAdsLayoutContext().bind(lvi2, ad2); } else{ int origPos = AdapterCalculator.getOriginalContentPosition(position, adFetcher.getFetchedAdsCount(), mAdapter.getItemCount()); mAdapter.onBindViewHolder(viewHolder, origPos); } }
public AppInstallAdViewHolder getAppInstallAdViewHolder() { // There is no instance in which this will be called before getView(), so there is // no reason getTag() should ever return a null value. AdViewHolder holder = (AdViewHolder) mFrame.getTag(); if (holder instanceof AppInstallAdViewHolder) { return (AppInstallAdViewHolder) holder; } else { mFrame.removeAllViews(); LayoutInflater inflater = (LayoutInflater) mFrame.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); NativeAppInstallAdView adView = (NativeAppInstallAdView) inflater .inflate(R.layout.item_app_install_ad, mFrame, false); mFrame.addView(adView); AppInstallAdViewHolder appInstallAdViewHolder = new AppInstallAdViewHolder(adView); mFrame.setTag(appInstallAdViewHolder); return appInstallAdViewHolder; } }
/** * Replaces the content in the views that make up the menu item view and the * Native Express ad view. This method is invoked by the layout manager. */ @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { int viewType = getItemViewType(position); switch (viewType) { case NATIVE_APP_INSTALL_AD_VIEW_TYPE: NativeAppInstallAd appInstallAd = (NativeAppInstallAd) mRecyclerViewItems.get(position); populateAppInstallAdView(appInstallAd, (NativeAppInstallAdView) holder.itemView); break; case NATIVE_CONTENT_AD_VIEW_TYPE: NativeContentAd contentAd = (NativeContentAd) mRecyclerViewItems.get(position); populateContentAdView(contentAd, (NativeContentAdView) holder.itemView); break; case MENU_ITEM_VIEW_TYPE: // fall through default: MenuItemViewHolder menuItemHolder = (MenuItemViewHolder) holder; MenuItem menuItem = (MenuItem) mRecyclerViewItems.get(position); // Get the menu item image resource ID. String imageName = menuItem.getImageName(); int imageResID = mContext.getResources().getIdentifier(imageName, "drawable", mContext.getPackageName()); // Add the menu item details to the menu item view. menuItemHolder.menuItemImage.setImageResource(imageResID); menuItemHolder.menuItemName.setText(menuItem.getName()); menuItemHolder.menuItemPrice.setText(menuItem.getPrice()); menuItemHolder.menuItemCategory.setText(menuItem.getCategory()); menuItemHolder.menuItemDescription.setText(menuItem.getDescription()); } }
private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd, NativeAppInstallAdView adView) { // Some assets are guaranteed to be in every NativeAppInstallAd. ((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon() .getDrawable()); ((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody()); ((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction()); // These assets aren't guaranteed to be in every NativeAppInstallAd, so it's important to // check before trying to display them. if (nativeAppInstallAd.getPrice() == null) { adView.getPriceView().setVisibility(View.INVISIBLE); } else { adView.getPriceView().setVisibility(View.VISIBLE); ((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice()); } if (nativeAppInstallAd.getStore() == null) { adView.getStoreView().setVisibility(View.INVISIBLE); } else { adView.getStoreView().setVisibility(View.VISIBLE); ((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore()); } if (nativeAppInstallAd.getStarRating() == null) { adView.getStarRatingView().setVisibility(View.INVISIBLE); } else { ((RatingBar) adView.getStarRatingView()) .setRating(nativeAppInstallAd.getStarRating().floatValue()); adView.getStarRatingView().setVisibility(View.VISIBLE); } // Assign native ad object to the native view. adView.setNativeAd(nativeAppInstallAd); }
@Override protected void prepare(@NonNull final NativeAppInstallAdView adView, @NonNull final NativeAppInstallAd nativeAd) { final TextView adTitle = (TextView)adView.findViewById(R.id.ad_title); final TextView adBody = (TextView)adView.findViewById(R.id.ad_body); final ImageView adImage = (ImageView)adView.findViewById(R.id.ad_image); final TextView adCallToAction = (TextView)adView.findViewById(R.id.ad_call_to_action); adView.setHeadlineView(adTitle); adView.setBodyView(adBody); adView.setImageView(adImage); adView.setCallToActionView(adCallToAction); }
/** * Creates or reuses a {@link FrameLayout} for this ad placement, filling it asynchronously * with the assets for its {@link com.google.android.gms.ads.formats.NativeAppInstallAd}. * * @param convertView a {@link View} to reuse if possible * @param parent The {@link ViewGroup} into which the {@link View} will be placed * @return a {@link FrameLayout} that contains or will contain the ad assets for this placement */ @Override public View getView(View convertView, ViewGroup parent) { // For each native ad, a FrameLayout is created and returned to the ListView as // the item's root view. This allows the fetcher to asynchronously request an ad, which // will be converted into a set of Views and placed into the FrameLayout by the // ViewHolder object. FrameLayout frameLayout = (FrameLayout) convertView; AppInstallAdViewHolder holder = (frameLayout == null) ? null : (AppInstallAdViewHolder) frameLayout.getTag(); // If the holder is null, either this is the first time the item has been displayed, or // convertView held a different type of item before. In either case, a new FrameLayout // and ViewHolder should be created and used. if (holder == null) { frameLayout = new FrameLayout(parent.getContext()); LayoutInflater inflater = (LayoutInflater) parent.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); NativeAppInstallAdView adView = (NativeAppInstallAdView) inflater .inflate(R.layout.item_app_install_ad, frameLayout, false); frameLayout.addView(adView); holder = new AppInstallAdViewHolder(adView); frameLayout.setTag(holder); } // This statement kicks off the ad loading process. The ViewHolder has some interface // methods that will be invoked when the ad is ready to be displayed or fails to load. mFetcher.loadAd(frameLayout.getContext(), holder); // The FrameLayout is returned to the ListView so it will have something to show for the // item right now. The FrameLayout will be filled with native ad assets later, once the // ad has finished loading. return frameLayout; }
/** * Stores the View for a {@link NativeAppInstallAd} and locates specific {@link View}s used * to display its assets. * * @param adView the {@link View} used to display assets for a native app install ad. */ public AppInstallAdViewHolder(NativeAppInstallAdView adView) { mAdView = adView; mAdView.setHeadlineView(mAdView.findViewById(R.id.appinstall_headline)); mAdView.setImageView(mAdView.findViewById(R.id.appinstall_image)); mAdView.setBodyView(mAdView.findViewById(R.id.appinstall_body)); mAdView.setCallToActionView(mAdView.findViewById(R.id.appinstall_call_to_action)); mAdView.setIconView(mAdView.findViewById(R.id.appinstall_app_icon)); mAdView.setPriceView(mAdView.findViewById(R.id.appinstall_price)); mAdView.setStarRatingView(mAdView.findViewById(R.id.appinstall_stars)); mAdView.setStoreView(mAdView.findViewById(R.id.appinstall_store)); }
/** * Populates a {@link NativeAppInstallAdView} object with data from a given * {@link NativeAppInstallAd}. * * @param nativeAppInstallAd the object containing the ad's assets * @param adView the view to be populated */ private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd, NativeAppInstallAdView adView) { VideoController videoController = nativeAppInstallAd.getVideoController(); // Assign native ad object to the native view. adView.setNativeAd(nativeAppInstallAd); adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline)); adView.setBodyView(adView.findViewById(R.id.appinstall_body)); adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action)); adView.setIconView(adView.findViewById(R.id.appinstall_app_icon)); adView.setPriceView(adView.findViewById(R.id.appinstall_price)); adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars)); adView.setStoreView(adView.findViewById(R.id.appinstall_store)); // Some assets are guaranteed to be in every NativeAppInstallAd. ((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody()); ((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction()); ((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon() .getDrawable()); MediaView sampleMediaView = adView.findViewById(R.id.appinstall_media); ImageView imageView = adView.findViewById(R.id.appinstall_image); if (videoController.hasVideoContent()) { imageView.setVisibility(View.GONE); adView.setMediaView(sampleMediaView); } else { sampleMediaView.setVisibility(View.GONE); adView.setImageView(imageView); List<NativeAd.Image> images = nativeAppInstallAd.getImages(); if (images.size() > 0) { ((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable()); } } // Some aren't guaranteed, however, and should be checked. if (nativeAppInstallAd.getPrice() == null) { adView.getPriceView().setVisibility(View.INVISIBLE); } else { adView.getPriceView().setVisibility(View.VISIBLE); ((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice()); } if (nativeAppInstallAd.getStore() == null) { adView.getStoreView().setVisibility(View.INVISIBLE); } else { adView.getStoreView().setVisibility(View.VISIBLE); ((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore()); } if (nativeAppInstallAd.getStarRating() == null) { adView.getStarRatingView().setVisibility(View.INVISIBLE); } else { ((RatingBar) adView.getStarRatingView()) .setRating(nativeAppInstallAd.getStarRating().floatValue()); adView.getStarRatingView().setVisibility(View.VISIBLE); } // Handle the fact that this could be a Sample SDK native ad, which includes a // "degree of awesomeness" field. Bundle extras = nativeAppInstallAd.getExtras(); if (extras.containsKey(SampleCustomEvent.DEGREE_OF_AWESOMENESS)) { TextView degree = (TextView) adView.findViewById(R.id.appinstall_degreeofawesomeness); degree.setVisibility(View.VISIBLE); degree.setText(extras.getString(SampleCustomEvent.DEGREE_OF_AWESOMENESS)); } }
@Override public void trackView(final View view) { view.post(new Runnable() { @Override public void run() { if (view instanceof NativeAppInstallAdView) { NativeAppInstallAdView appInstallAdView = (NativeAppInstallAdView) view; final ArrayList<View> assetViews = new ArrayList<>(); if (appInstallAdView.getHeadlineView() != null) { assetViews.add(appInstallAdView.getHeadlineView()); } if (appInstallAdView.getBodyView() != null) { assetViews.add(appInstallAdView.getBodyView()); } if (appInstallAdView.getCallToActionView() != null) { assetViews.add(appInstallAdView.getCallToActionView()); } if (appInstallAdView.getIconView() != null) { assetViews.add(appInstallAdView.getIconView()); } if (appInstallAdView.getImageView() != null) { assetViews.add(appInstallAdView.getImageView()); } if (appInstallAdView.getPriceView() != null) { assetViews.add(appInstallAdView.getPriceView()); } if (appInstallAdView.getStarRatingView() != null) { assetViews.add(appInstallAdView.getStarRatingView()); } if (appInstallAdView.getStoreView() != null) { assetViews.add(appInstallAdView.getStoreView()); } if (appInstallAdView.getMediaView() != null) { assetViews.add(mediaAdView); } nativeAd.registerView(view, assetViews); } else { Log.w(TAG, "Failed to register view for interaction."); } } }); }
@Override public void bind(NativeAdView nativeAdView, NativeAd nativeAd) throws ClassCastException{ if (nativeAdView == null || nativeAd == null) return; if(!(nativeAd instanceof NativeAppInstallAd) || !(nativeAdView instanceof NativeAppInstallAdView)) throw new ClassCastException(); NativeAppInstallAd ad = (NativeAppInstallAd) nativeAd; NativeAppInstallAdView adView = (NativeAppInstallAdView) nativeAdView; // Locate the view that will hold the headline, set its text, and call the // NativeAppInstallAdView's setHeadlineView method to register it. TextView tvHeader = (TextView) adView.findViewById(R.id.tvHeader); tvHeader.setText(ad.getHeadline()); adView.setHeadlineView(tvHeader); TextView tvDescription = (TextView) adView.findViewById(R.id.tvDescription); tvDescription.setText(ad.getBody()); adView.setBodyView(tvDescription); ImageView ivLogo = (ImageView) adView.findViewById(R.id.ivLogo); if(ad.getIcon()!=null) ivLogo.setImageDrawable(ad.getIcon().getDrawable()); adView.setIconView(ivLogo); Button btnAction = (Button) adView.findViewById(R.id.btnAction); btnAction.setText(ad.getCallToAction()); adView.setCallToActionView(btnAction); TextView tvStore = (TextView) adView.findViewById(R.id.tvStore); tvStore.setText(ad.getStore()); adView.setStoreView(tvStore); TextView tvPrice = (TextView) adView.findViewById(R.id.tvPrice); tvPrice.setText(ad.getPrice()); adView.setPriceView(tvPrice); ImageView ivImage = (ImageView) adView.findViewById(R.id.ivImage); if (ad.getImages() != null && ad.getImages().size() > 0) { ivImage.setImageDrawable(ad.getImages().get(0).getDrawable()); ivImage.setVisibility(View.VISIBLE); } else ivImage.setVisibility(View.GONE); adView.setImageView(ivImage); // Call the NativeAppInstallAdView's setNativeAd method to register the // NativeAd. adView.setNativeAd(ad); }
/** * To render ad view properly, implementation is expected to call {@link NativeAppInstallAdView#setHeadlineView(View)}, {@link NativeAppInstallAdView#setBodyView(View)}, {@link NativeAppInstallAdView#setCallToActionView(View)}, {@link NativeAppInstallAdView#setImageView(View)}, {@link NativeAppInstallAdView#setIconView(View)}, and/or {@link NativeAppInstallAdView#setStarRatingView(View)} methods, * and render the ad view text/images using {@link NativeAppInstallAd#getHeadline()}, {@link NativeAppInstallAd#getBody()}, {@link NativeAppInstallAd#getCallToAction()}, {@link NativeAppInstallAd#getImages()}, {@link NativeAppInstallAd#getIcon()}, and/or {@link NativeAppInstallAd#getStarRating()} methods. * @param adView The native ad view to render the ad. * @param nativeAd The native ad instance used to render the ad view. */ protected abstract void prepare(@NonNull NativeAppInstallAdView adView, @NonNull NativeAppInstallAd nativeAd);