Android - 数据更改后无法在RecyclerView上查找视图

时间:2017-06-18 03:47:08

标签: android android-recyclerview

我正在开发一个观看视频的应用程序。我的应用程序有一个列表视频和分页数据。在我将原生广告添加到列表之前,一切正常。我做了以下代码:

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);
        }
    });

代码将原生广告项目添加到列表数据:

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);
        }
    }
}

第一次,它显示得很好(包含视频项和原生广告)。但是当加载更多(转到下一页)时,它的代码行出错(cardView为null):

  

最终的CardView cardView =(CardView)findViewById(R.id.native_ads_card_view);

我已经调试过,发现recyclelerView的子视图没有原生广告项,尽管列表数据有。 这有什么问题?

0 个答案:

没有答案