在关闭插页式广告应用时移动到随机活动

时间:2017-12-12 18:13:31

标签: android android-recyclerview admob recycler-adapter

我创建了一个词汇应用程序,其中有一个包含类别名称的recyclerview,点击每个类别的新活动开始,显示该类别中的所有单词。在我放置InterstitialAd之间,关闭添加它将移动到该特定类别,但该应用程序移动到其他类别。如果没有加载广告,则会启动正确的活动。

这是类别适配器的代码:

公共类AllWordAdapter扩展了RecyclerView.Adapter {

private List<AllWordsModel> mCategoryList;
private Context context;
private String fragmentName;

private InterstitialAd mInterstitialAd;

public AllWordAdapter(List<AllWordsModel> mCategoryList, Context context,String fragmentName) {
    this.mCategoryList = mCategoryList;
    this.context = context;
    this.fragmentName = fragmentName;
}

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

@Override
public void onBindViewHolder(AllWordViewHolder holder, int position) {
    final AllWordsModel word = mCategoryList.get(position);
    holder.mCategory.setText(word.getmWordCategory());
    holder.mBackgroung.setImageResource(word.getmImageRes());

    if (fragmentName.equals("AllWordsFragment")){
        holder.mStar.setVisibility(View.GONE);
    }else {
        holder.mStar.setVisibility(View.VISIBLE);
    }

    final Activity activity = (Activity)context;

    mInterstitialAd = new InterstitialAd(activity);
    mInterstitialAd.setAdUnitId("ca-app-pub-9180372532679866/1983137539");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    holder.mAllWordLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (mInterstitialAd.isLoaded()){
                mInterstitialAd.show();
            }else {

                Intent myintent = new Intent(activity, WordListActivity.class);
                myintent.putExtra("Category", word.getmWordCategory());
                myintent.putExtra("Fragment",fragmentName);
                context.startActivity(myintent);

            }
        }
    });

    mInterstitialAd.setAdListener(new AdListener()

                                  {
                                      @Override
                                      public void onAdClosed() {
                                          Intent myintent = new Intent(activity, WordListActivity.class);
                                          myintent.putExtra("Category", word.getmWordCategory());
                                          myintent.putExtra("Fragment",fragmentName);
                                          context.startActivity(myintent);

                                          mInterstitialAd.loadAd(new AdRequest.Builder().build());
                                      }
                                  }

    );

}

@Override
public int getItemCount() {
    return mCategoryList.size();
}

}

0 个答案:

没有答案