2017-03-31 15 views
0

세 개의 탭이있는 탐색 모음이 있습니다. 각 탭은 조각입니다. 이제 두 번째 탭을 클릭하면 삽입 광고가 나옵니다. 그럼 광고를 어디에로드해야합니까? 그리고 광고를 어디에서 보여야합니까?전면 광고를 어떻게 표시합니까?

내 MainActivity :

public class MainActivity extends AppCompatActivity{ 
    public MediaPlayer mp; 
    NavigationView mNavigationView; 
    FragmentManager mFragmentManager; 
    FragmentTransaction mFragmentTransaction; 
    public InterstitialAd mInterstitialAd; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ; 

     mFragmentManager = getSupportFragmentManager(); 
     mFragmentTransaction = mFragmentManager.beginTransaction(); 
     mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit(); 



     android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); 



     AdRequest adRequest = new AdRequest.Builder().build(); 

     AdRequest adRequest = new AdRequest.Builder().build(); 
     mInterstitialAd = new InterstitialAd(getContext()); 
     mInterstitialAd.setAdUnitId("ca-app-pub-9756227769488050/2954649723"); 
     mInterstitialAd.loadAd(adRequest); 


    } 

내 secondFragment :

public class SecondFragment extends Fragment { 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView=inflater.inflate(R.layout.second_layout,container,false); 


     return rootView; 
    } 

그리고 (내가 그 중요한 경우 몰라요) 내 TabFragment : 내가 말을해야 할

public class TabFragment extends Fragment { 
    public static TabLayout tabLayout; 
    public static ViewPager viewPager; 
    public static int int_items = 3 ; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 


     /** 
     *Inflate tab_layout and setup Views. 
     */ 
      View x = inflater.inflate(R.layout.tab_layout,null); 
      tabLayout = (TabLayout) x.findViewById(R.id.tabs); 
      viewPager = (ViewPager) x.findViewById(R.id.viewpager); 

     /** 
     *Set an Apater for the View Pager 
     */ 
     viewPager.setAdapter(new MyAdapter(getChildFragmentManager())); 

     /** 
     * Now , this is a workaround , 
     * The setupWithViewPager dose't works without the runnable . 
     * Maybe a Support Library Bug . 
     */ 

     tabLayout.post(new Runnable() { 
      @Override 
      public void run() { 
        tabLayout.setupWithViewPager(viewPager); 
        } 
     }); 

     return x; 

    } 

    class MyAdapter extends FragmentPagerAdapter { 

     public MyAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     /** 
     * Return fragment with respect to Position . 
     */ 

     @Override 
     public Fragment getItem(int position) 
     { 

      if(position == 0){ 
       return new FirstFragment(); 
      } 
      if(position == 1){ 
       return new SecondFragment(); 
      } 
      if(position == 2){ 
       return new ThirdFragment(); 
      } 

     return null; 
     } 

     @Override 
     public int getCount() { 

      return int_items; 

     } 

     /** 
     * This method returns the title of the tab according to the position. 
     */ 

     @Override 
     public CharSequence getPageTitle(int position) { 

      switch (position){ 
       case 0 : 
        return "first"; 
       case 1 : 
        return "second"; 
       case 2 : 
        return "third"; 
      } 
       return null; 
     } 
    } 


     }); 
    } 

} 

그래서 showInterstitial()?

final public void showInterstitial(){ 
    mInterstitialAd.setAdListener(new AdListener() { 
     @Override 
     public void onAdLoaded() { 
      super.onAdLoaded(); 
      if(mInterstitialAd.isLoaded()){ 
       mInterstitialAd.show(); 
      } 

     } 

     @Override 
     public void onAdClosed() { 
     } 

Im이 문제에 대해 한 달 이상 걸려 있으니 자세히 설명해주세요. 감사합니다

+0

가능한 복제 [이 경우에 올바르게 삽입 광고를 표시하는 방법?] (http://stackoverflow.com/questions/43141684/how-to-show-interstitial-ad-correctly-in-this- 케이스) – William

답변

0

광고를 캐싱하기 시작한 것처럼 기본 활동에 load()를 남겨 두어야합니다. 그런 다음 어댑터 클릭 이벤트 (즉, 프래그먼트 처리 위치)를 처리하는 showInterstitial() 함수를 호출합니다.

+0

나는 안드로이드에 처음 온 경험이있다. ^^ –

0
public class MainActivity extends Activity { 
private InterstitialAd interstitial; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ; 

mFragmentManager = getSupportFragmentManager(); 
mFragmentTransaction = mFragmentManager.beginTransaction(); 
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit(); 

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); 

    // Prepare the Interstitial Ad 
    interstitial = new InterstitialAd(MainActivity.this); 
    // Insert the Ad Unit ID 
    interstitial.setAdUnitId("ca-app-pub-123456789/123456789"); 

    //Locate the Banner Ad in activity_main.xml 
    AdView adView = (AdView) this.findViewById(R.id.adView); 

    // Request for Ads 
    AdRequest adRequest = new AdRequest.Builder().build(); 

    // Load ads into Banner Ads 
    adView.loadAd(adRequest); 


} 
public void displayInterstitial() { 
    // If Ads are loaded, show Interstitial else show nothing. 
    // Load ads into Interstitial Ads 
    interstitial.loadAd(adRequest); 

    // Prepare an Interstitial Ad Listener 
    interstitial.setAdListener(new AdListener() { 
     public void onAdLoaded() { 
      if (interstitial.isLoaded()) { 
     interstitial.show(); 
    } 
     } 
    }); 
    } 
} 
+0

다시 한 번 내 질문을 읽어주세요 –

+0

내 편집 된 답변보기 및 두 번째 탭 클릭시'displayInterstitial()'호출 –