2017-12-06 10 views
0

フラグメントで新しいアクティビティに移動する多くのインテントを持つカスタムリサイクルビューがあります。コンセプトはこうです。ユーザーがrecyclerviewのアイテムをクリックすると、新しいアクティビティが表示される前にインタースティシャル広告を表示します。ユーザーが広告を閉じたり、広告の読み込みに失敗すると、自動的に広告が閉じられ、次のアクティビティが表示されます。フラグメント内のrecyclerviewアイテムをクリックした後にインタースティシャル広告を表示

私はフラグメントではなくアクティビティを使用しています。

これは私のコードは、これまで

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> { 


    public static class ViewHolder extends RecyclerView.ViewHolder { 

     private InterstitialAd interstitialAd; 

     private TextView homeTitle, homeDesc; 
     ImageView homeImage; 

     private Context context; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      createInterstitial(); 
      context = itemView.getContext(); 
      homeTitle = (TextView)itemView.findViewById(R.id.homeTitle); 
      homeDesc = (TextView)itemView.findViewById(R.id.homeDesc); 
      homeImage = (ImageView)itemView.findViewById(R.id.homeImage); 

      itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        final Intent intent; 
        switch (getAdapterPosition()) { 
         case 0: 
          showInterstitial(); 
          intent = new Intent(context, AllBlock.class); 
          break; 
         case 1: 
          showInterstitial(); 
          intent = new Intent(context, BasicBlock.class); 
          break; 

         default: 
          intent = new Intent(context, AllBlock.class); 
          break; 
        } 
        context.startActivity(intent); 
       } 
      }); 
     } 

     public void createInterstitial() { 
      interstitialAd = new InterstitialAd(context); 
      interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); 
      interstitialAd.setAdListener(new AdListener() { 
       @Override 
       public void onAdLoaded() { 

       } 
       @Override 
       public void onAdClosed() { 
        loadInterstitial(); 
       } 
      }); 
     } 

     public void loadInterstitial() { 
      AdRequest interstitialRequest = new AdRequest.Builder().build(); 
      interstitialAd.loadAd(interstitialRequest); 
     } 

     public void showInterstitial() { 
      if (interstitialAd.isLoaded()) { 
       interstitialAd.show(); 
      } else { 
       loadInterstitial(); 
      } 
     } 
    } 

答えて

0

あなたがしなければならない何かである:

1)特定のビューonClickListenerを抽出します。それをlistenerとしましょう。

2)View.OnClickListenerを実装する独自のクラスを作成します。

3)抽出したビューをクラスリスナーに追加します。

4)ビューのonClickListenerを自分のクラスに設定します。

5)あなたのクラスのonClickであなたのロジックを行い、次にノーマルビューのclickイベントを呼び出します。

+0

私はこのダムリクエストを知っていますが、コードを投稿できますか?私はそれが動作する場合は、あなたが適用されます:) – user9056633

+0

............... – user9056633

+0

私はそれを行うだろうしたら、上記のいくつかのステップのためにJavaの反射を使用する必要があります。私の答えをアップアップしてください:) – Whitebear

関連する問題