我已经在Fragment中创建了AdMob原生广告并且它可以正常工作但是在刷卡应用后总是请求新广告,我认为我创建了太多请求。
所以我的问题是我真的创造了太多的请求。如果答案是肯定的,那么我该如何解决?请使用一些代码,我在google上搜索了几个小时,但没有找到任何答案。如果答案是否定的话,那就没问题了:)
我的代码:
公共类ExerciseGuideActivity扩展了AppCompatActivity {
private ExerciseGuidePagerAdapter mAdapter;
private TabLayout mTabLayout;
private ViewPager mPager;
private View tag;
NativeExpressAdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise_guide);
mAdapter = new ExerciseGuidePagerAdapter(getSupportFragmentManager(), getApplicationContext());
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mTabLayout.setupWithViewPager(mPager);
mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
}
public static class MyFragment extends Fragment {
private static final java.lang.String ARG_PAGE = "arg_page";
public MyFragment() {
}
public static MyFragment newInstance(int pageNumber) {
MyFragment myFragment = new MyFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARG_PAGE, pageNumber);
myFragment.setArguments(arguments);
return myFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle arguments = getArguments();
int pageNumber = arguments.getInt(ARG_PAGE);
arguments.getInt(ARG_PAGE);
final View v;
if(pageNumber == 0) {
v = LayoutInflater.from(getContext()).inflate(R.layout.workout_1, null);
}else if(pageNumber == 1){
v = LayoutInflater.from(getContext()).inflate(R.layout.workout_2, null);
}else{
v = LayoutInflater.from(getContext()).inflate(R.layout.workout_3, null);
}
NativeExpressAdView adView = (NativeExpressAdView) v.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.rl_ads);
rl.setVisibility(RelativeLayout.VISIBLE);
}
});
return v;
}
}
}
寻呼机适配器
public class ExerciseGuidePagerAdapter extends FragmentStatePagerAdapter {
private Context context;
private String [] tabs = new String [] { "warm_up", "exercise", "cool_down" };
public ExerciseGuidePagerAdapter(FragmentManager fm, Context c) {
super(fm);
context = c;
}
@Override
public Fragment getItem(int position) {
ExerciseGuideActivity.MyFragment myFragment = ExerciseGuideActivity.MyFragment.newInstance(position);
return myFragment;
}
@Override
public int getCount() {
return tabs.length;
}
@Override
public CharSequence getPageTitle(int position) {
int get_resource = context.getResources().getIdentifier(tabs[position], "string", context.getPackageName());
return context.getString(get_resource);
}
}