【问题标题】:Is it possible to use admob native ads with StaggeredGridLayoutManager?是否可以将 admob 原生广告与 StaggeredGridLayoutManager 一起使用?
【发布时间】:2017-08-10 15:01:57
【问题描述】:

我关注firebase admob guides 将admob 高级原生广告添加到我的recyclerviewadapter。我可以轻松地展示中型和大型广告类型的广告。但我的应用程序还包括交错网格作为视图类型。但我无法以这种格式显示广告,我只能在广告行中看到白屏。我认为这是大小问题,但 admob 中没有交错行格式的大小。它限制了我 280dp 的最小宽度值。有没有办法以交错格式展示广告?如果没有,您能否建议我使用其他广告提供商(如 admob)来解决此问题?

【问题讨论】:

    标签: android android-recyclerview admob staggeredgridlayout


    【解决方案1】:

    感谢google groups answer 中的 Chris,我使用 GridLayoutManager 而不是 StaggeredGridLayoutManager 解决了这个问题。

    解决办法

      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,你可以找到其他类和布局。因为这是这个项目的修改版本。 我希望这个解决方案适用于像我这样的其他人。

    【讨论】:

    • 完美解决方案。!!
    • @kalpesh-mayani 以上两个链接都不存在,能否请您更新正确的链接?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2020-07-11
    相关资源
    最近更新 更多