如何在Tabbed-Fragment活动中显示admob Banner

时间:2017-02-24 21:27:26

标签: android android-fragments admob

我正在使用寻呼机视图显示 4个标签式片段 我想在每个标签更改时在底部显示横幅广告 我的导航抽屉里还有多个碎片。
当我打开导航抽屉中的任何片段时,我还想展示横幅广告。
我在片段中使用 Recycler View ,在每个片段中使用显示对象列表
我添加了admob广告依赖性,在清单中添加了代码 我只想知道 adView 应该放在哪里,以显示每个片段中的横幅,无论是在标签中还是独立片段。

xml of MainActivity

<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>  

标签符号
这将控制所有选项卡式活动。

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
这保存了视图寻呼机和标签< 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"/>

2 个答案:

答案 0 :(得分:0)

将此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" />

答案 1 :(得分:0)

我已经通过编辑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>