如何在标签碎片活动中显示Admob横幅



我正在显示 4个带有Pager View 的选项卡式片段。
我想在底部显示横幅广告当每个选项卡都更改时。
我的导航抽屉中也有多个碎片。
我还想在从导航抽屉中打开任何片段时显示横幅广告。
我在片段中使用 recycler View 在每个片段中显示对象的列表。
我添加了Admob广告的依赖性,在清单中添加了代码。
我只想知道 adview 在何处放置在每个片段中的横幅,无论其在标签中还是独立的片段。

MainActivity的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ToolbarColoredBackArrow"
    app:title="Drawer With Swipe Tabs" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
       android:layout_height="match_parent"
        android:orientation="vertical"/>
    <android.support.design.widget.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/shitstuff"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginTop="4dp"
        android:background="#000000"
        android:fitsSystemWindows="true"
        app:itemIconTint="#fbc500"
        app:itemTextColor="#FFFFFF"
        app:menu="@menu/drawermenu" />
</android.support.v4.widget.DrawerLayout></LinearLayout>  

tab frament
这控制了所有选项卡活动。

public class TabFragment extends Fragment {
  public static TabLayout tabLayout;
  public static ViewPager viewPager;
  public static int int_items = 4;
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View x = inflater.inflate(R.layout.tab_layout, null);
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (ViewPager) x.findViewById(R.id.viewpager);
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    viewPager.setOffscreenPageLimit(int_items - 1);
    tabLayout.post(new Runnable() {
      @Override
      public void run() {
        tabLayout.setupWithViewPager(viewPager);
      }
    });
    return x;
  }
  class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
      super(fm);
    }
    @Override
    public Fragment getItem(int position) {
      switch (position) {
        case 0:
          return new Fragment1();
        case 1:
          return new Fragment2();
        case 2:
          return new Fragment3();
        case 3:
          return new Fragment4();
      }
      return null;
    }
    @Override
    public int getCount() {
      return int_items;
    }
    @Override
    public CharSequence getPageTitle(int position) {
      switch (position) {
        case 0:
          return "Tab1";
        case 1:
          return "Tab2";
        case 2:
          return "Tab3";
        case 3:
          return "Tab4";
      }
      return null;
    }
  }
}

xml tablayout
这保留了查看Pager和Tabs的ID

< LinearLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: app = "http://schemas.android.com/apk/res-auto"
android: layout_width = "match_parent"
android: orientation = "vertical"
android: layout_height = "wrap_content" >
<android.support.design.widget.TabLayout
android: id = "@+id/tabs"
app: tabGravity = "center"
app: tabMode = "scrollable"
android: layout_width = "match_parent"
android: layout_height = "wrap_content" >
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android: id = "@+id/viewpager"
android: layout_width = "match_parent"
android: layout_height = "match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>

所有片段活动都像这样
片段活动

public class TopNewsFragment extends Fragment {
  public TopNewsFragment() {}
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_cheese_list, container, false);
    setHasOptionsMenu(true);
    layoutManager = new LinearLayoutManager(this.getActivity());
    recyclerAdapter = new RecyclerAdapter(getActivity(), new ArrayList < Values > ());
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(recyclerAdapter);

    return rootView;
  }

最后片段布局:
fragment_cheese_list

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

用您的现有代码替换此XML代码。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
<FrameLayout
    android:id="@+id/containerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:weight="1"
    android:orientation="vertical"/>
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_home_footer">
</com.google.android.gms.ads.AdView>
</LinearLayout>
<android.support.design.widget.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/shitstuff"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginTop="4dp"
    android:background="#000000"
    android:fitsSystemWindows="true"
    app:itemIconTint="#fbc500"
    app:itemTextColor="#FFFFFF"
    app:menu="@menu/drawermenu" />

我通过编辑fragment_cheese_list.xml这样解决了问题:

<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="55dp" />
<com.google.android.gms.ads.AdView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/ad_view"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id"/>
</RelativeLayout>

最新更新