如何通过单击列表中通过适配器的项目来调用插页式广告



我需要通过单击列表中的来自我的适配器的项目来调用此活动中的插页式广告。

public class SoundRecyclerAdapter extends RecyclerView.Adapter<SoundRecyclerAdapter.SoundViewHolder> {
    private final ArrayList<SoundObject> soundObjects;
    SoundRecyclerAdapter(ArrayList<SoundObject> soundObjects) {
        this.soundObjects = soundObjects;
    }
    @NonNull
    @Override
    public SoundViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.sound_item, parent, false);
        return new SoundViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(@NonNull SoundViewHolder holder, int position) {
        final SoundObject object = soundObjects.get(position);
        final Integer soundID = object.getItemID();
        holder.itemTextView.setText(object.getItemName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // I tried calling the interstitial here, but to no avail
                EventHandlerClass.startMediaPlayer(v, soundID);
            }
        });
        // Handle actions when the user presses a sound button
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                EventHandlerClass.popupManager(v, object);
                return true;
            }
        });
    }
    @Override
    public int getItemCount() {
        return soundObjects.size();
    }
    class SoundViewHolder extends RecyclerView.ViewHolder {
        final TextView itemTextView;
        SoundViewHolder(View itemView) {
            super(itemView);
            itemTextView = itemView.findViewById(R.id.textViewItem);
        }
    }
}

将上下文作为适配器构造函数的参数传递:

private Context context;
SoundRecyclerAdapter(Context context, ArrayList<SoundObject> soundObjects) {
    this.soundObjects = soundObjects;
    this.context = context;
}

并在点击处理程序中使用它

setOnClickListener() 方法中使用以下代码:

mInterstitialAd = new InterstitialAd(SoundRecyclerAdapter.this);

然后添加广告请求并加载广告。

最新更新