1

**我在 RecyclerView 中显示原生广告 ** RecyclerView 中有 2 种类型的项目,一种是 NativeExpressAdView,第 2 项是 Some Object。我在每 3 个对象之后在列表中添加了 Ads 项目。它工作正常,直到我删除列表中的项目并在 RecyclerView 中调用 notifyDataSetChanged(),在调用 notifyDataSetChanged() 后,每个对象的位置都发生了变化并且代码变得混乱,请查看代码并告诉我我能做些什么来解决这个问题。

// A menu item view type.
private static final int MENU_ITEM_VIEW_TYPE = 0;

// The Native Express ad view type.
private static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1;

public DailyAlarmAdapter(Context context, List<Object> givenPostAndAds, int day) {
    mContext = context;
    alarmList = new ArrayList<>(givenPostAndAds);
    db = new DatabaseHandler(mContext);
    this.day = day;
}

/**
 * Determines the view type for the given position.
 */
@Override
public int getItemViewType(int position) {
    // Show list of post
    return (position % NavMainActivity.ITEMS_PER_AD == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE
            : MENU_ITEM_VIEW_TYPE;

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    // Post layout
    switch (viewType) {
        case MENU_ITEM_VIEW_TYPE:
            View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                    R.layout.class_design, viewGroup, false);
            //menuItemLayoutView.setOnClickListener(this);
            ListingHolder holder = new ListingHolder(menuItemLayoutView);
            menuItemLayoutView.setTag(holder);
            return holder;
        case NATIVE_EXPRESS_AD_VIEW_TYPE:
            // fall through
        default:
            View nativeExpressLayoutView = LayoutInflater.from(
                    viewGroup.getContext()).inflate(R.layout.native_express_ad_container,
                    viewGroup, false);
            return new NativeExpressAdViewHolder(nativeExpressLayoutView);
    }

}

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


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    int viewType = getItemViewType(position);
    switch (viewType) {
        case MENU_ITEM_VIEW_TYPE:
            **//Here Item type is Menu Item but now at this position there is NativeExpressAdView**
            if (alarmList.get(position) instanceof NativeExpressAdView)
                return;
            ListingHolder listingHolder = (ListingHolder) holder;
            configurePostView(listingHolder, position);
            break;
        case NATIVE_EXPRESS_AD_VIEW_TYPE:
        default:
            **//Here Item type is AD_VIEW_TYPE but now at this position there is Alarm Object**
            if (alarmList.get(position) instanceof Alarm)
                return;
            NativeExpressAdViewHolder nativeExpressHolder =
                    (NativeExpressAdViewHolder) holder;
            NativeExpressAdView adView =
                    (NativeExpressAdView) alarmList.get(position);
            ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView;
            // The NativeExpressAdViewHolder recycled by the RecyclerView may be a different
            // instance than the one used previously for this position. Clear the
            // NativeExpressAdViewHolder of any subviews in case it has a different
            // AdView associated with it, and make sure the AdView for this position doesn't
            // already have a parent of a different recycled NativeExpressAdViewHolder.
            if (adCardView.getChildCount() > 0) {
                adCardView.removeAllViews();
            }
            if (adView.getParent() != null) {
                ((ViewGroup) adView.getParent()).removeView(adView);
            }

            // Add the Native Express ad to the native express ad view.
            adCardView.addView(adView);
    }

}
4

0 回答 0