如何在回收者视图中为本地广告添加notifyItemRangeInserted

时间:2020-09-23 09:11:43

标签: android android-recyclerview unifiednativeadview

我已经在recyclerView内实现了本机高级广告,并且仅在加载native ad时显示回收者视图项,因此我执行了以下方法。

在我的方法中,显示了回收站视图项目,并且在native advanced ad加载了Adapter.notifyDataSetChanged();后,这会更新整个回收站视图。

那么我如何在下面的方法中将notifyItemRangeInserted用于原生广告。

MainActivity

@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_folder_videos);
    ButterKnife.bind(this);

    populateFolders();

    loadNativeAds();

    loadMenu();
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private void populateFolders() {
    Bundle bundle = getIntent().getExtras();
    String folderName = bundle.getString("folderName");
    setTitle(folderName);

    for (int i = 0; i < Constant.allMediaList.size(); i++) {
        if (folderName.equals(new File(String.valueOf(Constant.allMediaList.get(i))).getParentFile().getName())) {
            FolderVideoPath.add(Constant.allMediaList.get(i).getAbsolutePath());
        }
    }

    String[] path = new String[FolderVideoPath.size()];
    FolderVideoPath.toArray(path);
    if (FolderVideoPath.size() < 1) {
        NUMBER_OF_ADS = 1;
    } if (FolderVideoPath.size() >= 10) {
        NUMBER_OF_ADS = 2;
    } if (FolderVideoPath.size() > 22){
        NUMBER_OF_ADS = 3;
    }
}

private void loadNativeAds() {
    AdLoader.Builder builder = new AdLoader.Builder(this, getString(R.string.native_ad_id));
    adLoader = builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            mNativeAds.add(unifiedNativeAd);
            if (!adLoader.isLoading()) {
                insertAdsInMenuItems();
            }
        }
    }).withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            if (!adLoader.isLoading()) {
                insertAdsInMenuItems();
            }
        }
    }).build();
    adLoader.loadAds(new AdRequest.Builder().build(), NUMBER_OF_ADS);
}

public ArrayList<Object> getRecyclerViewItems() {
    return FolderVideoPath;
}

private void insertAdsInMenuItems() {
    if (mNativeAds.size() <= 0) {
        return;
    }
    int offset = (FolderVideoPath.size() / mNativeAds.size()) + 1;
    //int offset = (6) + 1;
    int index = /*FolderVideoPath.size() > 4 ? 4 :*/ 0;
    for (UnifiedNativeAd ad : mNativeAds) {
        FolderVideoPath.add(index, ad);
        index = index + offset;
    }
    loadMenu();
}

private void loadMenu() {
    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            Object recyclerViewItem = FolderVideoPath.get(position);
            if (recyclerViewItem instanceof UnifiedNativeAd) {
                return 2;
            }
            return 1;
        }
    });
    recyclerVideos.setLayoutManager(new LinearLayoutManager(this));
    recyclerVideos.setHasFixedSize(true);
    folderVideoRVAdapter = new FolderVideoRVAdapter(this, FolderVideoPath);
    recyclerVideos.setAdapter(folderVideoRVAdapter);
    folderVideoRVAdapter.notifyItemRangeInserted(0,offset);
    folderVideoRVAdapter.notifyDataSetChanged();

}

RecyclerViewAdapter

class FolderVideoRVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int MENU_ITEM_VIEW_TYPE = 0;
    private static final int UNIFIED_NATIVE_AD_VIEW_TYPE = 1;

    private final Context mContext;
    private final ArrayList<Object> videosPath;

    public FolderVideoRVAdapter(Context context, ArrayList<Object> videosPath) {
        this.mContext = context;
        this.videosPath = videosPath;
    }

    public class FolderVideoAdapterViewHolder extends RecyclerView.ViewHolder{
              ...........
        }
    }

    @Override
    public int getItemCount() {
        return videosPath.size();
    }

    @Override
    public int getItemViewType(int position) {
        Object recyclerViewItem = videosPath.get(position);
        if (recyclerViewItem instanceof UnifiedNativeAd) {
            return UNIFIED_NATIVE_AD_VIEW_TYPE;
        }
        return MENU_ITEM_VIEW_TYPE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        switch (viewType) {
            case UNIFIED_NATIVE_AD_VIEW_TYPE:
                View unifiedNativeLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.small_ads, viewGroup, false);
                return new UnifiedNativeAdViewHolder(unifiedNativeLayoutView);
            case MENU_ITEM_VIEW_TYPE:
            default:
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.files_list, viewGroup, false);
                return new FolderVideoAdapterViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        int viewType = getItemViewType(position);
        switch (viewType) {
            case UNIFIED_NATIVE_AD_VIEW_TYPE:
                UnifiedNativeAd nativeAd = (UnifiedNativeAd) videosPath.get(position);
                populateNativeAdView(nativeAd, ((UnifiedNativeAdViewHolder) holder).getAdView());
                break;
            case MENU_ITEM_VIEW_TYPE:
            default:
                
                Glide.with(mContext)
                        .load(videosPath.get(position))
                        .apply(options)
                        .into(((FolderVideoAdapterViewHolder) holder).thumbnail);
                ((FolderVideoAdapterViewHolder) holder).videoName.setText("" + FilenameUtils.getBaseName((String) videosPath.get(position)));
        }
    }

    private void populateNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView) {
        ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
        ((TextView) adView.getBodyView()).setText(nativeAd.getBody());
        ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
    }
}

那我如何使用notifyItemRangeInserted并插入广告而不刷新整个活动?

0 个答案:

没有答案