是否可以在StaggeredGridLayoutManager中使用admob原生广告?

时间:2017-08-10 15:01:58

标签: android android-recyclerview admob staggeredgridlayout

我按照firebase admob guides将admob高级原生广告添加到我的recyclerviewadapter。我可以轻松地展示中型和大型广告类型的广告。但我的应用程序还包括交错网格作为视图类型。但我不能以这种格式显示广告,我只能在广告行中看到白色屏幕。我认为这是尺寸问题,但在admob中没有交错行格式的大小。它限制了我最小宽度值280dp。有没有办法以交错格式展示广告?如果没有,你可以告诉我另一个像admob这样的广告提供商来解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

我使用GridLayoutManager而不是StaggeredGridLayoutManager解决了这个问题,这要归功于此google groups answer中的Chris。

这是解决方案

  public static final int ITEMS_PER_AD = 8;

  private GridLayoutManager mLayoutManager;

  // The Native Express ad height.
  private static final int NATIVE_EXPRESS_AD_HEIGHT = 150;

 // The Native Express ad unit ID.
 private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1072772517";

 // The RecyclerView that holds and displays Native Express ads and menu items.
 private RecyclerView mRecyclerView;

 // List of Native Express ads and MenuItems that populate the RecyclerView.
 private List<Object> mRecyclerViewItems = new ArrayList<>();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLayoutManager = new GridLayoutManager(this, 2);
    mLayoutManager.setSpanSizeLookup(new 
    GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        if (position % MainActivity.ITEMS_PER_AD == 0) {
            return 2;
        }
        return 1;
    }
    });

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    // Use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView.
    mRecyclerView.setHasFixedSize(true);

    // Specify a linear layout manager.
    mRecyclerView.setLayoutManager(mLayoutManager);

   // Update the RecyclerView item's list with menu items and Native Express ads.
   addMenuItemsFromJson();
   addNativeExpressAds();
   setUpAndLoadNativeExpressAds();

   // Specify an adapter.
   RecyclerView.Adapter adapter = new RecyclerViewAdapter(this, mRecyclerViewItems);
   mRecyclerView.setAdapter(adapter);
}

如果您可以关注this sample project,则可以找到其他课程和布局。因为这是这个项目的修改版本。 我希望这个解决方案适用于像我这样的人。