【发布时间】:2017-02-24 21:27:25
【问题描述】:
我正在显示 4 个带有分页器视图的选项卡式片段。
我想在每个标签发生变化时在底部显示横幅广告。
此外,我的导航抽屉中有多个片段。
我还想在从导航抽屉中打开任何片段时显示横幅广告。
我在片段中使用 Recycler 视图,在每个片段中使用 显示对象列表。
我已经添加了 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>
标签框架
这控制所有选项卡式活动。
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 选项卡布局
这包含视图寻呼机和选项卡的 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>
所有的 Fragments Activity 都是这样的
Fragment Activity
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"/>
【问题讨论】:
标签: android android-fragments admob