You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在RecyclerView条目点击时展示ironSource插屏广告

在RecyclerView条目点击时展示ironSource插屏广告的实现方案

1. 确保ironSource SDK已正确集成

先确认项目完成基础集成,以Android为例:
在build.gradle(Module level)添加依赖(替换为最新版本):

dependencies {
    implementation 'com.ironsource.sdk:mediationsdk:7.1.12'
}

在Application类或目标Activity的onCreate中初始化SDK:

@Override
public void onCreate() {
    super.onCreate();
    // 替换为你的官方APP_KEY
    IronSource.init(this, "YOUR_APP_KEY", IronSource.AD_UNIT.INTERSTITIAL);
}

2. 预加载插屏广告并设置状态监听

插屏广告需提前加载才能实现点击即展示,建议在Activity/Fragment的onStart中启动加载并监听状态:

private InterstitialListener mInterstitialListener = new InterstitialListener() {
    @Override
    public void onInterstitialAdReady() {
        // 广告就绪,可标记状态供后续判断
    }

    @Override
    public void onInterstitialAdLoadFailed(IronSourceError error) {
        // 加载失败,可记录错误日志或尝试重新加载
    }

    @Override
    public void onInterstitialAdClosed() {
        // 广告关闭后立即加载下一个,保证下次点击有可用广告
        IronSource.loadInterstitial();
    }

    @Override
    public void onInterstitialAdShowFailed(IronSourceError error) {
        // 展示失败,可提示用户或触发重新加载
    }

    // 其余回调可根据业务需求实现
    @Override
    public void onInterstitialAdOpened() {}
    @Override
    public void onInterstitialAdClicked() {}
};

@Override
protected void onStart() {
    super.onStart();
    IronSource.setInterstitialListener(mInterstitialListener);
    // 启动首次广告加载
    IronSource.loadInterstitial();
}

3. 处理RecyclerView条目点击事件

两种常见实现方式,按需选择:

方式一:适配器内绑定点击回调

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<ItemModel> mData;
    private OnItemClickListener mClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public MyAdapter(List<ItemModel> data, OnItemClickListener listener) {
        mData = data;
        mClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // 绑定数据逻辑
        holder.itemView.setOnClickListener(v -> {
            if (mClickListener != null) {
                mClickListener.onItemClick(position);
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}

在Activity中实现回调并触发广告展示:

MyAdapter adapter = new MyAdapter(dataList, position -> {
    // 检查广告是否就绪
    if (IronSource.isInterstitialReady()) {
        IronSource.showInterstitial();
    } else {
        Toast.makeText(this, "广告加载中,请稍后再试", Toast.LENGTH_SHORT).show();
        IronSource.loadInterstitial();
    }
});
recyclerView.setAdapter(adapter);

方式二:通过ItemTouchListener全局监听

recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    private final GestureDetector gestureDetector = new GestureDetector(MainActivity.this,
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (childView != null) {
                        int position = recyclerView.getChildAdapterPosition(childView);
                        // 检查广告状态并展示
                        if (IronSource.isInterstitialReady()) {
                            IronSource.showInterstitial();
                        } else {
                            Toast.makeText(MainActivity.this, "广告加载中,请稍后再试", Toast.LENGTH_SHORT).show();
                            IronSource.loadInterstitial();
                        }
                    }
                    return true;
                }
            });

    @Override
    public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
        return gestureDetector.onTouchEvent(e);
    }

    @Override
    public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {}
    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
});

关键注意事项

  • 必须预加载广告,避免点击时临时加载导致的延迟或失败;
  • 广告关闭后务必调用IronSource.loadInterstitial(),保证后续点击有可用广告;
  • 处理加载/展示失败的异常场景,提升用户体验;
  • 确保AndroidManifest.xml已添加必要权限:INTERNET、ACCESS_NETWORK_STATE。

内容的提问来源于stack exchange,提问作者ManavPatni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 09:57:37