0

I am developing an application that watches videos. My app has a list videos and paging data. Everything is ok until I add native ads to list. I have done as following code:

recyclerView.post(new Runnable() {
        @Override
        public void run() {
            final float scale = ListMostPopularByCountryActivity.this.getResources().getDisplayMetrics().density;
            // Set the ad size and ad unit ID for each Native Express ad in the items list.
            for (int i = 0; i < mListVideo.size(); i += ITEMS_PER_AD) {
                if ((mListVideo.get(i) instanceof NativeExpressAdView)) {
                    final NativeExpressAdView adView = (NativeExpressAdView) mListVideo.get(i);
                    final CardView cardView = (CardView) findViewById(R.id.native_ads_card_view);
                    if (ViewCompat.isLaidOut(cardView)) {
                        final int adWidth = cardView.getWidth() - cardView.getPaddingLeft() - cardView.getPaddingRight();
                        AdSize adSize = new AdSize((int) (adWidth / scale), NATIVE_EXPRESS_AD_HEIGHT);
                        adView.setAdSize(adSize);
                        adView.setAdUnitId(Constant.ADS_NATIVE_ID);
                    }
                }
            }
            // Load the first Native Express ad in the items list.
            loadNativeExpressAd(0);
        }
    });

The code add native ads item to list data:

private void addNativeExpressAds() {

    // Loop through the items array and place a new Native Express ad in every ith position in
    // the items List.
    for (int i = 0; i < mListVideo.size(); i += ITEMS_PER_AD) {
        final NativeExpressAdView adView = new NativeExpressAdView(ListMostPopularByCountryActivity.this);
        if (!(mListVideo.get(i) instanceof NativeExpressAdView)) {
            mListVideo.add(i, adView);
        }
    }
}

The first time, it displayed well (contains video item and native ads). But when load more (go to next page), it got error by the line of code (cardView is null):

final CardView cardView = (CardView) findViewById(R.id.native_ads_card_view);

I have debugged and found that childview of recyclerView don't have native ads item although list data has it. What is wrong with this?

Huy Nguyen
  • 1,032
  • 14
  • 38
  • what is your code to go to next page? – pouyan Jun 18 '17 at 09:12
  • I custom a scroll listener to detect like this article: https://stackoverflow.com/questions/26543131/how-to-implement-endless-list-with-recyclerview The paging part was working fine, I sure for that. By many codes, I can not post here. When going to next page, I have checked data, it has added data from the new page. – Huy Nguyen Jun 18 '17 at 09:18
  • i want to know that how do you add your new item to your recycler – pouyan Jun 18 '17 at 09:19
  • I have following this guide. https://github.com/googleads/googleads-mobile-android-examples/tree/master/advanced/NativeExpressRecyclerViewExample – Huy Nguyen Jun 18 '17 at 09:27

0 Answers0