2017-08-10 77 views
0

我遵循firebase admob guides將admob高級原生廣告添加到我的recyclerviewadapter。我可以輕鬆展示中型和大型廣告類型的廣告。但我的應用程序還包括交錯網格視圖類型。但是我無法展示這種格式的廣告,我只能看到廣告一排的白色屏幕。我認爲這是尺寸問題,但在admob中沒有交錯排列格式的大小。它將我的最小寬度值限制在280dp。有沒有什麼辦法以交錯格式顯示廣告?如果沒有,比你能告訴我像admob這樣的另一個廣告提供商來解決這個問題嗎?是否可以在StaggeredGridLayoutManager中使用admob原生廣告?

回答

0

感謝Chris在這個google groups answer,我使用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,你可以找到其他類和佈局。因爲這是這個項目的修改版本。 我希望這個解決方案能夠像我這樣的人工作。

相關問題