ifmvo / TogetherAd

🔥持续更新。Android广告聚合:帮助 Android 开发者快速、便捷、灵活的接入国内多家主流安卓广告 SDK。广点通(优量汇)、穿山甲、快手联盟、百青藤(百度Mob)。开屏广告、Banner横幅广告、插屏广告、激励广告、原生信息流、全屏广告。
MIT License
806 stars 174 forks source link

RecyclerView中使用信息流 #30

Closed lurongshuang closed 3 years ago

lurongshuang commented 3 years ago

RecyclerView中使用信息流: 穿山甲原生模板广告功能 什么时候可以更新一下呢?

lurongshuang commented 3 years ago

我将代码进行如下改动,兼容 原生模板,期待大佬更新 新版本。

public class CustomCsjProvider extends CsjProvider { @Override public void getNativeAdList(@NotNull Activity activity, @NotNull String adProviderType, @NotNull String alias, int maxCount, @NotNull NativeListener listener) { if (Native.INSTANCE.getNativeAdType() == -1) { throw new IllegalArgumentException("|--------------------------------------------------------------------------------------\n" + " | 必须在每次请求穿山甲的原生广告之前设置类型。\n" + " | 设置方式:\n" + " | CsjProvider.Native.nativeAdType = AdSlot.TYPE_XXX(类型和你的广告位ID一致)。\n" + " | CsjProvider.Native.nativeAdType = AdSlot.TYPE_FEED\n" + " | CsjProvider.Native.nativeAdType = AdSlot.TYPE_INTERACTION_AD\n" + " | CsjProvider.Native.nativeAdType = AdSlot.TYPE_BANNER\n" + " | CsjProvider.Native.nativeAdType = AdSlot.TYPE_CACHED_SPLASH\n" + " | CsjProvider.Native.nativeAdType = AdSlot.TYPE_DRAW_FEED\n" + " | CsjProvider.Native.nativeAdType = AdSlot.TYPE_FULL_SCREEN_VIDEO\n" + " | CsjProvider.Native.nativeAdType = AdSlot.TYPE_REWARD_VIDEO\n" + " | CsjProvider.Native.nativeAdType = AdSlot.TYPE_SPLASH\n" + " | 任选其一\n" + " |--------------------------------------------------------------------------------------"); } callbackFlowStartRequest(adProviderType, listener);

    //设置广告参数
    AdSlot adSlot = new AdSlot.Builder()
            .setCodeId(TogetherAdCsj.INSTANCE.getIdMapCsj().get(alias))
            .setSupportDeepLink(Native.INSTANCE.getSupportDeepLink())
            .setAdCount(maxCount)
            .setExpressViewAcceptedSize(Native.INSTANCE.getImageAcceptedSizeWidth$csj_release(), Native.INSTANCE.getImageAcceptedSizeHeight$csj_release())
            .setImageAcceptedSize(Native.INSTANCE.getImageAcceptedSizeWidth$csj_release(), Native.INSTANCE.getImageAcceptedSizeHeight$csj_release())
            .build();

    TTAdSdk.getAdManager().createAdNative(activity).loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
        @Override
        public void onError(int code, String message) {
            callbackFlowFailed(adProviderType, listener, "错误码: " + code + "}, 错误信息" + message);
        }

        @Override
        public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
            if (ads == null || ads.size() == 0) {
                callbackFlowFailed(adProviderType, listener, "请求成功,但是返回的list为空");
                return;
            }
            callbackFlowLoaded(adProviderType, listener, ads);
        }
    });

}

@Override
public boolean nativeAdIsBelongTheProvider(@NotNull Object adObject) {
    return true;

}

public class NativeViewCsjSimple6 extends BaseNativeView { @Override public void showNative(@NotNull String s, @NotNull Object o, @NotNull ViewGroup viewGroup, @Nullable NativeViewListener nativeViewListener) { Log.e("", ""); com.bytedance.sdk.openadsdk.core.nativeexpress.k k = (com.bytedance.sdk.openadsdk.core.nativeexpress.k) o; // viewGroup.removeAllViews(); if (k.getExpressAdView().getParent() == null) { viewGroup.addView(k.getExpressAdView()); } } }

public class NativeTemplateSimple5 extends BaseNativeTemplate {

    @Nullable
    @Override
    public BaseNativeView getNativeView(@NotNull String s) {
        if (AdProviderType.GDT.getType().equals(s)) {
            return new NativeViewGdtSimple5();

        } else if (AdProviderType.CSJ.getType().equals(s)) {
            return new NativeViewCsjSimple6();

        }

// else if(AdProviderType.BAIDU.getType().equals(s)){ // return new NativeViewBaiduSimple5(); // } return null; } }

CustomCsjProvider.Native.INSTANCE.setNativeAdType(AdSlot.TYPE_FEED); adHelperNativeRv.getList(new NativeListener() { @Override public void onAdLoaded(@NotNull String s, @NotNull List<?> list) { bindAdListener((List) list); for (Object o : list) { myAdapter.addData(new TopListBean(o)); } }

        @Override
        public void onAdStartRequest(@NotNull String s) {
            //每个提供商请求之前都会回调
        }

        @Override
        public void onAdFailedAll() {
            //所有的提供商都失败
        }

        @Override
        public void onAdFailed(@NotNull String s, @Nullable String s1) {
            //单个提供商请求失败

        }
    });

private void bindAdListener(final List ads) { final int count = myAdapter.getData().size(); for (TTNativeExpressAd ad : ads) { final TTNativeExpressAd adTmp = ad; myAdapter.addData(new TopListBean(adTmp));

        adTmp.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int type) {
                ToastUtils.s(getActivity(), "广告被点击");
            }

            @Override
            public void onAdShow(View view, int type) {
                ToastUtils.s(getActivity(), "广告展示");
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                ToastUtils.s(getActivity(), msg + " code:" + code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                //返回view的宽高 单位 dp
                ToastUtils.s(getActivity(), "渲染成功");
                myAdapter.notifyDataSetChanged();
            }
        });
        ad.render();

    }

}