android|Android穿山甲SDK接入信息流广告

listView信息流

public class ProjectsAdapter extends BaseAdapter {private static final int ITEM_VIEW_TYPE_NORMAL = 0; private static final int ITEM_VIEW_TYPE_GROUP_PIC_AD = 1; private static final int ITEM_VIEW_TYPE_SMALL_PIC_AD = 2; private static final int ITEM_VIEW_TYPE_LARGE_PIC_AD = 3; private static final int ITEM_VIEW_TYPE_VIDEO = 4; private static final int ITEM_VIEW_TYPE_VERTICAL_IMG = 5; //竖版图片 private static final int ITEM_VIEW_TYPE_VIDEO_VERTICAL = 6; //竖版视频private int mVideoCount = 0; private List mData; private Context mContext; private Map mTTAppDownloadListenerMap = new WeakHashMap<>(); public ProjectsAdapter(Context context, List data) { this.mContext = context; this.mData = https://www.it610.com/article/data; }@Override public int getCount() { return mData.size(); // for test }@Overridepublic TTNativeExpressAd getItem(int position) { return mData.get(position).getNativeExpressAd(); }@Override public long getItemId(int position) { return position; }//信息流广告的样式,有大图、小图、组图和视频,通过ad.getImageMode()来判断 @Override public int getItemViewType(int position) {TTNativeExpressAd ad= getItem(position); /*TTNativeExpressAd ad = null; if(bean!=null){ ad = bean.getNativeExpressAd(); }*/ if (ad == null) { return ITEM_VIEW_TYPE_NORMAL; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_SMALL_IMG) { return ITEM_VIEW_TYPE_SMALL_PIC_AD; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_LARGE_IMG) { return ITEM_VIEW_TYPE_LARGE_PIC_AD; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_GROUP_IMG) { return ITEM_VIEW_TYPE_GROUP_PIC_AD; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO) { return ITEM_VIEW_TYPE_VIDEO; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_VERTICAL_IMG) { return ITEM_VIEW_TYPE_VERTICAL_IMG; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO_VERTICAL || ad.getImageMode() == TTAdConstant.IMAGE_MODE_LIVE) { return ITEM_VIEW_TYPE_VIDEO_VERTICAL; } else { TToast.show(mContext,"图片展示样式错误"); return ITEM_VIEW_TYPE_NORMAL; } }@Override public View getView(int position, View convertView, ViewGroup parent) {TTNativeExpressAd ad = getItem(position); /*TTNativeExpressAd ad = null; if(bean!=null){ ad = bean.getNativeExpressAd(); }*/switch (getItemViewType(position)) { case ITEM_VIEW_TYPE_SMALL_PIC_AD: case ITEM_VIEW_TYPE_LARGE_PIC_AD: case ITEM_VIEW_TYPE_GROUP_PIC_AD: case ITEM_VIEW_TYPE_VERTICAL_IMG: case ITEM_VIEW_TYPE_VIDEO: case ITEM_VIEW_TYPE_VIDEO_VERTICAL: return getVideoView(convertView, parent, ad); default: return getNormalView(convertView, parent, position); } }//渲染视频广告,以视频广告为例,以下说明 @SuppressWarnings("RedundantCast")private View getVideoView(View convertView, ViewGroup parent, @NonNull final TTNativeExpressAd ad) { final AdViewHolder adViewHolder; try { if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_ad_native_express, parent, false); adViewHolder = new AdViewHolder(); adViewHolder.videoView = (FrameLayout) convertView.findViewById(R.id.iv_listitem_express); convertView.setTag(adViewHolder); } else { adViewHolder = (AdViewHolder) convertView.getTag(); }//绑定广告数据、设置交互回调 bindData(convertView, adViewHolder, ad); if (adViewHolder.videoView != null) { //获取视频播放view,该view SDK内部渲染,在媒体平台可配置视频是否自动播放等设置。View video = ad.getExpressAdView(); if (video != null) { adViewHolder.videoView.removeAllViews(); if (video.getParent() == null) { adViewHolder.videoView.addView(video); //ad.render(); } } } } catch (Exception e) { e.printStackTrace(); }return convertView; }/** * 非广告list * @param convertView * @param parent * @param position * @return */ @SuppressWarnings("RedundantCast") @SuppressLint("SetTextI18n") private View getNormalView(View convertView, ViewGroup parent, int position) { NormalViewHolder normalViewHolder; if (convertView == null) { View v = LayoutInflater.from(mContext).inflate(R.layout.item_price, parent, false); normalViewHolder = new NormalViewHolder(v); convertView = v; convertView.setTag(normalViewHolder); } else { normalViewHolder = (NormalViewHolder) convertView.getTag(); } ProjectsItem bean = mData.get(position); Glide.with(mContext).load(bean.getImgProjectMain()).into(normalViewHolder.image); normalViewHolder.introduction.setText(bean.getShortIntro()); normalViewHolder.titleName.setText(bean.getName()); normalViewHolder.views.setText(bean.getTesterNum() + " 人测过"); return convertView; }/** * 设置广告的不喜欢,注意:强烈建议设置该逻辑,如果不设置dislike处理逻辑,则模板广告中的 dislike区域不响应dislike事件。 * * @param ad * @param customStyle 是否自定义样式,true:样式自定义 */private void bindDislike(final TTNativeExpressAd ad, boolean customStyle) { if (customStyle) { //使用自定义样式 DislikeInfo dislikeInfo = ad.getDislikeInfo(); if (dislikeInfo == null || dislikeInfo.getFilterWords() == null || dislikeInfo.getFilterWords().isEmpty()) { return; } final DislikeDialog dislikeDialog = new DislikeDialog(mContext, dislikeInfo); dislikeDialog.setOnDislikeItemClick(new DislikeDialog.OnDislikeItemClick() { @Override public void onItemClick(FilterWord filterWord) { //屏蔽广告 TToast.show(mContext, "点击 " + filterWord.getName()); //用户选择不喜欢原因后,移除广告展示 mData.remove(ad); notifyDataSetChanged(); } }); dislikeDialog.setOnPersonalizationPromptClick(new DislikeDialog.OnPersonalizationPromptClick() { @Override public void onClick(PersonalizationPrompt personalizationPrompt) { TToast.show(mContext, "点击了为什么看到此广告"); } }); ad.setDislikeDialog(dislikeDialog); return; } //使用默认模板中默认dislike弹出样式ad.setDislikeCallback((Activity) mContext, new TTAdDislike.DislikeInteractionCallback() { @Override public void onShow() {}@Override public void onSelected(int position, String value, boolean enforce) { TToast.show(mContext, "点击 " + value); //用户选择不喜欢原因后,移除广告展示 if (enforce) { mData.remove(ad); notifyDataSetChanged(); TToast.show(mContext, "NativeExpressListActivity 模版信息流 sdk强制移除View "); return; } mData.remove(ad); notifyDataSetChanged(); }@Override public void onCancel() { TToast.show(mContext, "点击取消 "); }}); }private void bindData(View convertView, final AdViewHolder adViewHolder, TTNativeExpressAd ad) { //设置dislike弹窗 bindDislike(ad, false); switch (ad.getInteractionType()) {case TTAdConstant.INTERACTION_TYPE_DOWNLOAD: bindDownloadListener(adViewHolder, ad); break; } }private void bindDownloadListener(final AdViewHolder adViewHolder, TTNativeExpressAd ad) { TTAppDownloadListener downloadListener = new TTAppDownloadListener() { private boolean mHasShowDownloadActive = false; @Override public void onIdle() { if (!isValid()) { return; } TToast.show(mContext, "点击广告开始下载"); }@SuppressLint("SetTextI18n") @Override public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) { if (!isValid()) { return; } if (!mHasShowDownloadActive) { mHasShowDownloadActive = true; TToast.show(mContext, appName + " 下载中,点击暂停", Toast.LENGTH_LONG); } }@SuppressLint("SetTextI18n") @Override public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) { if (!isValid()) { return; } TToast.show(mContext, appName + " 下载暂停", Toast.LENGTH_LONG); }@Override public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) { if (!isValid()) { return; } TToast.show(mContext, appName + " 下载失败,重新下载", Toast.LENGTH_LONG); }@Override public void onInstalled(String fileName, String appName) { if (!isValid()) { return; } TToast.show(mContext, appName + " 安装完成,点击打开", Toast.LENGTH_LONG); }@Override public void onDownloadFinished(long totalBytes, String fileName, String appName) { if (!isValid()) { return; } TToast.show(mContext, appName + " 下载成功,点击安装", Toast.LENGTH_LONG); }@SuppressWarnings("BooleanMethodIsAlwaysInverted") private boolean isValid() { return mTTAppDownloadListenerMap.get(adViewHolder) == this; } }; //一个ViewHolder对应一个downloadListener, isValid判断当前ViewHolder绑定的listener是不是自己 ad.setDownloadListener(downloadListener); // 注册下载监听器 mTTAppDownloadListenerMap.put(adViewHolder, downloadListener); }private static class AdViewHolder { FrameLayout videoView; }private static class NormalViewHolder { TextView titleName; TextView introduction; TextView views; CornerImageView image; public NormalViewHolder(View itemView) {image = itemView.findViewById(R.id.image); titleName = (TextView) itemView.findViewById(R.id.titleName); introduction = (TextView) itemView.findViewById(R.id.introduction); views = (TextView) itemView.findViewById(R.id.views); } }}

listitem_ad_native_express文件

recyclerView信息流
public class ClassifyListAdapter extends RecyclerView.Adapter { List mData; private Map mTTAppDownloadListenerMap = new WeakHashMap<>(); Context mContext; private static final String TAG = "ClassifyListAdapter"; private static final int FOOTER_VIEW_COUNT = 1; private static final int ITEM_VIEW_TYPE_LOAD_MORE = -1; private static final int ITEM_VIEW_TYPE_NORMAL = 0; private static final int ITEM_VIEW_TYPE_GROUP_PIC_AD = 1; private static final int ITEM_VIEW_TYPE_SMALL_PIC_AD = 2; private static final int ITEM_VIEW_TYPE_LARGE_PIC_AD = 3; private static final int ITEM_VIEW_TYPE_VIDEO = 4; private static final int ITEM_VIEW_TYPE_VERTICAL_PIC_AD = 5; //竖版图片int layoutId; public ClassifyListAdapter(Context context, int layoutId, List datas) { this.mContext = context; mRequestManager = Glide.with(mContext); this.mData = https://www.it610.com/article/datas; this.layoutId = layoutId; }@Override public int getItemCount() { int count = mData == null ? 0 : mData.size(); return count + FOOTER_VIEW_COUNT; }@NonNull@Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { RecyclerView.ViewHolder holder = null; switch (viewType) { case ITEM_VIEW_TYPE_LOAD_MORE: return new LoadMoreViewHolder(new LoadMoreView(mContext)); case ITEM_VIEW_TYPE_SMALL_PIC_AD: return new SmallAdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.listitem_ad_small_pic, parent, false)); case ITEM_VIEW_TYPE_LARGE_PIC_AD: // return new LargeAdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.listitem_ad_large_pic, parent, false)); case ITEM_VIEW_TYPE_VERTICAL_PIC_AD: return new VerticalAdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.listitem_ad_vertical_pic, parent, false)); case ITEM_VIEW_TYPE_GROUP_PIC_AD: // return new GroupAdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.listitem_ad_group_pic, parent, false)); case ITEM_VIEW_TYPE_VIDEO: // return new VideoAdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.listitem_ad_large_video, parent, false)); default: return new NormalViewHolder(LayoutInflater.from(mContext).inflate(layoutId, parent, false)); } }@SuppressLint("SetTextI18n") @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { int count = mData.size(); TTFeedAd ttFeedAd; try { if (holder instanceof SmallAdViewHolder) { ttFeedAd = mData.get(position).getFeedAd(); SmallAdViewHolder smallAdViewHolder = (SmallAdViewHolder) holder; ArrayList images = new ArrayList<>(); images.add(((SmallAdViewHolder) holder).mSmallImage); bindData(smallAdViewHolder, images, ttFeedAd); if (ttFeedAd.getImageList() != null && !ttFeedAd.getImageList().isEmpty()) {TTImage image = ttFeedAd.getImageList().get(0); if (image != null && image.isValid()) { mRequestManager.load(image.getImageUrl()).into(smallAdViewHolder.mSmallImage); } }} else if (holder instanceof LargeAdViewHolder) { ttFeedAd = mData.get(position).getFeedAd(); LargeAdViewHolder largeAdViewHolder = (LargeAdViewHolder) holder; ArrayList images = new ArrayList<>(); images.add(((LargeAdViewHolder) holder).mLargeImage); bindData(largeAdViewHolder, images, ttFeedAd); if (ttFeedAd.getImageList() != null && !ttFeedAd.getImageList().isEmpty()) { TTImage image = ttFeedAd.getImageList().get(0); if (image != null && image.isValid()) { mRequestManager.load(image.getImageUrl()).into(largeAdViewHolder.mLargeImage); } }} else if (holder instanceof VerticalAdViewHolder) { ttFeedAd = mData.get(position).getFeedAd(); VerticalAdViewHolder verticalAdViewHolder = (VerticalAdViewHolder) holder; ArrayList images = new ArrayList<>(); images.add((((VerticalAdViewHolder) holder).mVerticalImage)); bindData(verticalAdViewHolder, images, ttFeedAd); if (ttFeedAd.getImageList() != null && !ttFeedAd.getImageList().isEmpty()) { TTImage image = ttFeedAd.getImageList().get(0); if (image != null && image.isValid()) { mRequestManager.load(image.getImageUrl()).into(verticalAdViewHolder.mVerticalImage); } } } else if (holder instanceof GroupAdViewHolder) { ttFeedAd = mData.get(position).getFeedAd(); GroupAdViewHolder groupAdViewHolder = (GroupAdViewHolder) holder; ArrayList images = new ArrayList<>(); images.add(((GroupAdViewHolder) holder).mGroupImage1); images.add(((GroupAdViewHolder) holder).mGroupImage2); images.add(((GroupAdViewHolder) holder).mGroupImage3); bindData(groupAdViewHolder, images, ttFeedAd); if (ttFeedAd.getImageList() != null && ttFeedAd.getImageList().size() >= 3) { TTImage image1 = ttFeedAd.getImageList().get(0); TTImage image2 = ttFeedAd.getImageList().get(1); TTImage image3 = ttFeedAd.getImageList().get(2); if (image1 != null && image1.isValid()) { mRequestManager.load(image1.getImageUrl()).into(groupAdViewHolder.mGroupImage1); } if (image2 != null && image2.isValid()) { mRequestManager.load(image2.getImageUrl()).into(groupAdViewHolder.mGroupImage2); } if (image3 != null && image3.isValid()) { mRequestManager.load(image3.getImageUrl()).into(groupAdViewHolder.mGroupImage3); } }} else if (holder instanceof VideoAdViewHolder) { ttFeedAd = mData.get(position).getFeedAd(); final VideoAdViewHolder videoAdViewHolder = (VideoAdViewHolder) holder; ArrayList images = new ArrayList<>(); images.add(((VideoAdViewHolder) holder).videoView); bindData(videoAdViewHolder, images, ttFeedAd); ttFeedAd.setVideoAdListener(new TTFeedAd.VideoAdListener() { @Overridepublic void onVideoLoad(TTFeedAd ad) {}@Override public void onVideoError(int errorCode, int extraCode) {}@Overridepublic void onVideoAdStartPlay(TTFeedAd ad) {}@Overridepublic void onVideoAdPaused(TTFeedAd ad) {}@Overridepublic void onVideoAdContinuePlay(TTFeedAd ad) {}@Override public void onProgressUpdate(long current, long duration) {}@Overridepublic void onVideoAdComplete(TTFeedAd ad) {} }); if (videoAdViewHolder.videoView != null) {View video = ttFeedAd.getAdView(); videoAdViewHolder.videoView.post(new Runnable() { @Override public void run() { int width = videoAdViewHolder.videoView.getWidth(); int videoWidth = ttFeedAd.getAdViewWidth(); int videoHeight = ttFeedAd.getAdViewHeight(); // 根据广告View的宽高比,将adViewHolder.videoView的高度动态改变 UIUtils.setViewSize(videoAdViewHolder.videoView, width, (int) (width / (videoWidth / (double) videoHeight))); } }); if (video != null) { if (video.getParent() == null) { videoAdViewHolder.videoView.removeAllViews(); videoAdViewHolder.videoView.addView(video); } } }} else if (holder instanceof NormalViewHolder) { NormalViewHolder normalViewHolder = (NormalViewHolder) holder; BannerBean bean = mData.get(position); Glide.with(mContext).load(bean.getImage()).into(normalViewHolder.image); normalViewHolder.introduction.setText(bean.getIntroduction()); normalViewHolder.titleName.setText(bean.getTitle()); normalViewHolder.views.setText(bean.getViews() + " 人测过"); normalViewHolder.itemView.setOnClickListener(new MyOnClick(onItemClick,position,normalViewHolder.itemView)); } else if (holder instanceof LoadMoreViewHolder) { LoadMoreViewHolder loadMoreViewHolder = (LoadMoreViewHolder) holder; }if (holder instanceof LoadMoreViewHolder) { holder.itemView.setBackgroundColor(Color.TRANSPARENT); } else if (holder instanceof SmallAdViewHolder || holder instanceof VideoAdViewHolder || holder instanceof LargeAdViewHolder || holder instanceof GroupAdViewHolder || holder instanceof VerticalAdViewHolder) { holder.itemView.setBackgroundColor(Color.WHITE); } else { // holder.itemView.setBackgroundColor(getColorRandom()); } } catch (Exception e) { LogUtils.v(e.toString()); } }private int getColorRandom() { int a = Double.valueOf(Math.random() * 255).intValue(); int r = Double.valueOf(Math.random() * 255).intValue(); int g = Double.valueOf(Math.random() * 255).intValue(); int b = Double.valueOf(Math.random() * 255).intValue(); return Color.argb(a, r, g, b); }@Override public int getItemViewType(int position) { if (mData != null) { int count = mData.size(); if (position >= count) { return ITEM_VIEW_TYPE_LOAD_MORE; } else {TTFeedAd ad = mData.get(position).getFeedAd(); if (ad == null) { return ITEM_VIEW_TYPE_NORMAL; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_SMALL_IMG) { return ITEM_VIEW_TYPE_SMALL_PIC_AD; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_LARGE_IMG) { return ITEM_VIEW_TYPE_LARGE_PIC_AD; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_GROUP_IMG) { return ITEM_VIEW_TYPE_GROUP_PIC_AD; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO || ad.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO_VERTICAL || ad.getImageMode() == TTAdConstant.IMAGE_MODE_LIVE) { return ITEM_VIEW_TYPE_VIDEO; } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_VERTICAL_IMG) {//竖版图片 return ITEM_VIEW_TYPE_VERTICAL_PIC_AD; } else { TToast.show(mContext, "图片展示样式错误"); return ITEM_VIEW_TYPE_NORMAL; } }} return super.getItemViewType(position); }@Override public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); /* RecyclerView.LayoutManager layout = recyclerView.getLayoutManager(); if (layout != null && layout instanceof GridLayoutManager) { final GridLayoutManager manager = (GridLayoutManager) layout; manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { int type = getItemViewType(position); if (type == ITEM_VIEW_TYPE_LOAD_MORE || type == ITEM_VIEW_TYPE_VIDEO) { return manager.getSpanCount(); } return 1; } }); }*/ }@Override public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) { //noinspection unchecked super.onViewAttachedToWindow(holder); /* ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) { int position = holder.getLayoutPosition(); int type = getItemViewType(position); if (type == ITEM_VIEW_TYPE_LOAD_MORE || type == ITEM_VIEW_TYPE_VIDEO) { StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; p.setFullSpan(true); } }*/ }@SuppressWarnings({"CanBeFinal", "WeakerAccess"}) private static class LoadMoreViewHolder extends RecyclerView.ViewHolder { TextView mTextView; ProgressBar mProgressBar; @SuppressWarnings("RedundantCast") public LoadMoreViewHolder(View itemView) { super(itemView); itemView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)); mTextView = (TextView) itemView.findViewById(R.id.tv_load_more_tip); mProgressBar = (ProgressBar) itemView.findViewById(R.id.pb_load_more_progress); } }private static class AdViewHolder extends RecyclerView.ViewHolder { ImageView mIcon; ImageView mDislike; TextView mCreativeButton; TextView mTitle; TextView mDescription; TextView mSource; TextView mStopButton; TextView mRemoveButton; public AdViewHolder(View itemView) { super(itemView); } }private RequestManager mRequestManager; private void bindData(final AdViewHolder adViewHolder, List images, TTFeedAd ad) { //设置dislike弹窗 bindDislikeAction(adViewHolder.mDislike, ad, false); //可以被点击的view, 也可以把convertView放进来意味item可被点击 List clickViewList = new ArrayList<>(); clickViewList.add(adViewHolder.itemView); //触发创意广告的view(点击下载或拨打电话) List creativeViewList = new ArrayList<>(); creativeViewList.add(adViewHolder.mCreativeButton); //如果需要点击图文区域也能进行下载或者拨打电话动作,请将图文区域的view传入 //creativeViewList.add(convertView); //重要! 这个涉及到广告计费,必须正确调用。convertView必须使用ViewGroup。ad.registerViewForInteraction((ViewGroup) adViewHolder.itemView, images, clickViewList, creativeViewList, adViewHolder.mDislike, new TTNativeAd.AdInteractionListener() { @Overridepublic void onAdClicked(View view, TTNativeAd ad) { if (ad != null) { TToast.show(mContext, "广告" + ad.getTitle() + "被点击"); } }@Overridepublic void onAdCreativeClick(View view, TTNativeAd ad) { if (ad != null) { TToast.show(mContext, "广告" + ad.getTitle() + "被创意按钮被点击"); } }@Overridepublic void onAdShow(TTNativeAd ad) { if (ad != null) { TToast.show(mContext, "广告" + ad.getTitle() + "展示"); } } }); adViewHolder.mTitle.setText(ad.getTitle()); adViewHolder.mDescription.setText(ad.getDescription()); adViewHolder.mSource.setText(ad.getSource() == null ? "广告来源" : ad.getSource()); TTImage icon = ad.getIcon(); if (icon != null && icon.isValid()) { mRequestManager.load(icon.getImageUrl()).into(adViewHolder.mIcon); } TextView adCreativeButton = adViewHolder.mCreativeButton; switch (ad.getInteractionType()) {case TTAdConstant.INTERACTION_TYPE_DOWNLOAD: //如果初始化ttAdManager.createAdNative(getApplicationContext())没有传入activity 则需要在此传activity,否则影响使用Dislike逻辑 if (mContext instanceof Activity) { ad.setActivityForDownloadApp((Activity) mContext); } adCreativeButton.setVisibility(View.VISIBLE); adViewHolder.mStopButton.setVisibility(View.VISIBLE); adViewHolder.mRemoveButton.setVisibility(View.VISIBLE); bindDownloadListener(adCreativeButton, adViewHolder, ad); //绑定下载状态控制器 bindDownLoadStatusController(adViewHolder, ad); break; case TTAdConstant.INTERACTION_TYPE_DIAL: adCreativeButton.setVisibility(View.VISIBLE); adCreativeButton.setText("立即拨打"); adViewHolder.mStopButton.setVisibility(View.GONE); adViewHolder.mRemoveButton.setVisibility(View.GONE); break; case TTAdConstant.INTERACTION_TYPE_LANDING_PAGE:case TTAdConstant.INTERACTION_TYPE_BROWSER: //adCreativeButton.setVisibility(View.GONE); adCreativeButton.setVisibility(View.VISIBLE); adCreativeButton.setText("查看详情"); adViewHolder.mStopButton.setVisibility(View.GONE); adViewHolder.mRemoveButton.setVisibility(View.GONE); break; default: adCreativeButton.setVisibility(View.GONE); adViewHolder.mStopButton.setVisibility(View.GONE); adViewHolder.mRemoveButton.setVisibility(View.GONE); TToast.show(mContext, "交互类型异常"); } }/** * 绑定dislike逻辑 * * @param dislike * @param ad * @param isCustomDislike 是否使用自定义dislike 默认false */private void bindDislikeAction(View dislike, final TTFeedAd ad, boolean isCustomDislike) { if (isCustomDislike) { // 使用自定义Dislike final DislikeInfo dislikeInfo = ad.getDislikeInfo(); if (dislikeInfo == null || dislikeInfo.getFilterWords() == null || dislikeInfo.getFilterWords().isEmpty()) { return; } final DislikeDialog dislikeDialog = new DislikeDialog(mContext, dislikeInfo); dislikeDialog.setOnDislikeItemClick(new DislikeDialog.OnDislikeItemClick() { @Override public void onItemClick(FilterWord filterWord) { mData.remove(ad); notifyDataSetChanged(); } }); dislikeDialog.setOnPersonalizationPromptClick(new DislikeDialog.OnPersonalizationPromptClick() { @Override public void onClick(PersonalizationPrompt personalizationPrompt) { TToast.show(mContext, "点击了为什么看到此广告"); } }); ad.setDislikeDialog(dislikeDialog); dislike.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dislikeDialog.show(); } }); } else {final TTAdDislike ttAdDislike = ad.getDislikeDialog((Activity) mContext); if (ttAdDislike != null) {ad.getDislikeDialog((Activity) mContext).setDislikeInteractionCallback(new TTAdDislike.DislikeInteractionCallback() { @Override public void onShow() {}@Override public void onSelected(int position, String value, boolean enforce) { mData.remove(ad); notifyDataSetChanged(); if (enforce) { TToast.show(mContext, "FeedRecyclerActivity 原生信息流 sdk强制移除View "); } }@Override public void onCancel() {}}); } dislike.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ttAdDislike.showDislikeDialog(); } }); } }private void bindDownloadListener(final TextView adCreativeButton, final AdViewHolder adViewHolder, TTFeedAd ad) { TTAppDownloadListener downloadListener = new TTAppDownloadListener() { @Override public void onIdle() { if (!isValid()) { return; } adCreativeButton.setText("开始下载"); adViewHolder.mStopButton.setText("开始下载"); }@SuppressLint("SetTextI18n") @Override public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) { if (!isValid()) { return; } if (totalBytes <= 0L) { adCreativeButton.setText("0%"); } else { adCreativeButton.setText((currBytes * 100 / totalBytes) + "%"); } adViewHolder.mStopButton.setText("下载中"); }@SuppressLint("SetTextI18n") @Override public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) { if (!isValid()) { return; } if (totalBytes <= 0L) { adCreativeButton.setText("0%"); } else { adCreativeButton.setText((currBytes * 100 / totalBytes) + "%"); } adViewHolder.mStopButton.setText("下载暂停"); }@Override public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) { if (!isValid()) { return; } adCreativeButton.setText("重新下载"); adViewHolder.mStopButton.setText("重新下载"); }@Override public void onInstalled(String fileName, String appName) { if (!isValid()) { return; } adCreativeButton.setText("点击打开"); adViewHolder.mStopButton.setText("点击打开"); }@Override public void onDownloadFinished(long totalBytes, String fileName, String appName) { if (!isValid()) { return; } adCreativeButton.setText("点击安装"); adViewHolder.mStopButton.setText("点击安装"); }@SuppressWarnings("BooleanMethodIsAlwaysInverted") private boolean isValid() { return mTTAppDownloadListenerMap.get(adViewHolder) == this; } }; //一个ViewHolder对应一个downloadListener, isValid判断当前ViewHolder绑定的listener是不是自己 ad.setDownloadListener(downloadListener); // 注册下载监听器 mTTAppDownloadListenerMap.put(adViewHolder, downloadListener); }private void bindDownLoadStatusController(AdViewHolder adViewHolder, final TTFeedAd ad) { final DownloadStatusController controller = ad.getDownloadStatusController(); adViewHolder.mStopButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (controller != null) { controller.changeDownloadStatus(); TToast.show(mContext, "改变下载状态"); Log.d(TAG, "改变下载状态"); } } }); adViewHolder.mRemoveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (controller != null) { controller.cancelDownload(); TToast.show(mContext, "取消下载"); Log.d(TAG, "取消下载"); } } }); }@SuppressWarnings("WeakerAccess") private static class VideoAdViewHolder extends AdViewHolder { @SuppressWarnings("CanBeFinal") FrameLayout videoView; @SuppressWarnings("RedundantCast") public VideoAdViewHolder(View itemView) { super(itemView); mDislike = (ImageView) itemView.findViewById(R.id.iv_listitem_dislike); mTitle = (TextView) itemView.findViewById(R.id.tv_listitem_ad_title); mDescription = (TextView) itemView.findViewById(R.id.tv_listitem_ad_desc); mSource = (TextView) itemView.findViewById(R.id.tv_listitem_ad_source); videoView = (FrameLayout) itemView.findViewById(R.id.iv_listitem_video); mIcon = (ImageView) itemView.findViewById(R.id.iv_listitem_icon); mCreativeButton = (TextView) itemView.findViewById(R.id.btn_listitem_creative); mStopButton = (TextView) itemView.findViewById(R.id.btn_listitem_stop); mRemoveButton = (TextView) itemView.findViewById(R.id.btn_listitem_remove); } }private static class LargeAdViewHolder extends AdViewHolder { ImageView mLargeImage; @SuppressWarnings("RedundantCast") public LargeAdViewHolder(View itemView) { super(itemView); mDislike = (ImageView) itemView.findViewById(R.id.iv_listitem_dislike); mTitle = (TextView) itemView.findViewById(R.id.tv_listitem_ad_title); mDescription = (TextView) itemView.findViewById(R.id.tv_listitem_ad_desc); mSource = (TextView) itemView.findViewById(R.id.tv_listitem_ad_source); mLargeImage = (ImageView) itemView.findViewById(R.id.iv_listitem_image); mIcon = (ImageView) itemView.findViewById(R.id.iv_listitem_icon); mCreativeButton = (TextView) itemView.findViewById(R.id.btn_listitem_creative); mStopButton = (TextView) itemView.findViewById(R.id.btn_listitem_stop); mRemoveButton = (TextView) itemView.findViewById(R.id.btn_listitem_remove); } }private static class NormalViewHolder extends RecyclerView.ViewHolder { TextView titleName; TextView introduction; TextView views; CornerImageView image; @SuppressWarnings("RedundantCast") public NormalViewHolder(View itemView) { super(itemView); image = itemView.findViewById(R.id.image); titleName = (TextView) itemView.findViewById(R.id.titleName); introduction = (TextView) itemView.findViewById(R.id.introduction); views = (TextView) itemView.findViewById(R.id.views); } }private static class SmallAdViewHolder extends AdViewHolder { ImageView mSmallImage; @SuppressWarnings("RedundantCast") public SmallAdViewHolder(View itemView) { super(itemView); mDislike = (ImageView) itemView.findViewById(R.id.iv_listitem_dislike); mTitle = (TextView) itemView.findViewById(R.id.tv_listitem_ad_title); mSource = (TextView) itemView.findViewById(R.id.tv_listitem_ad_source); mDescription = (TextView) itemView.findViewById(R.id.tv_listitem_ad_desc); mSmallImage = (ImageView) itemView.findViewById(R.id.iv_listitem_image); mIcon = (ImageView) itemView.findViewById(R.id.iv_listitem_icon); mCreativeButton = (TextView) itemView.findViewById(R.id.btn_listitem_creative); mStopButton = (TextView) itemView.findViewById(R.id.btn_listitem_stop); mRemoveButton = (TextView) itemView.findViewById(R.id.btn_listitem_remove); } }private static class VerticalAdViewHolder extends AdViewHolder { ImageView mVerticalImage; @SuppressWarnings("RedundantCast") public VerticalAdViewHolder(View itemView) { super(itemView); mDislike = (ImageView) itemView.findViewById(R.id.iv_listitem_dislike); mTitle = (TextView) itemView.findViewById(R.id.tv_listitem_ad_title); mSource = (TextView) itemView.findViewById(R.id.tv_listitem_ad_source); mDescription = (TextView) itemView.findViewById(R.id.tv_listitem_ad_desc); mVerticalImage = (ImageView) itemView.findViewById(R.id.iv_listitem_image); mIcon = (ImageView) itemView.findViewById(R.id.iv_listitem_icon); mCreativeButton = (TextView) itemView.findViewById(R.id.btn_listitem_creative); mStopButton = (TextView) itemView.findViewById(R.id.btn_listitem_stop); mRemoveButton = (TextView) itemView.findViewById(R.id.btn_listitem_remove); } }@SuppressWarnings("CanBeFinal") private static class GroupAdViewHolder extends AdViewHolder { ImageView mGroupImage1; ImageView mGroupImage2; ImageView mGroupImage3; @SuppressWarnings("RedundantCast") public GroupAdViewHolder(View itemView) { super(itemView); mDislike = (ImageView) itemView.findViewById(R.id.iv_listitem_dislike); mTitle = (TextView) itemView.findViewById(R.id.tv_listitem_ad_title); mSource = (TextView) itemView.findViewById(R.id.tv_listitem_ad_source); mDescription = (TextView) itemView.findViewById(R.id.tv_listitem_ad_desc); mGroupImage1 = (ImageView) itemView.findViewById(R.id.iv_listitem_image1); mGroupImage2 = (ImageView) itemView.findViewById(R.id.iv_listitem_image2); mGroupImage3 = (ImageView) itemView.findViewById(R.id.iv_listitem_image3); mIcon = (ImageView) itemView.findViewById(R.id.iv_listitem_icon); mCreativeButton = (TextView) itemView.findViewById(R.id.btn_listitem_creative); mStopButton = (TextView) itemView.findViewById(R.id.btn_listitem_stop); mRemoveButton = (TextView) itemView.findViewById(R.id.btn_listitem_remove); } } public OnItemClick onItemClick; public void setOnItemClick( OnItemClick onItemClick){ this.onItemClick=onItemClick; } }

xml文件可以复制sdk中的xml,样式可以自己修改

【android|Android穿山甲SDK接入信息流广告】

    推荐阅读