1

我已经使用 recycler view 实现了一个网格布局。我需要在网格项目之间实现原生广告。我试过了,但我在我的 gridview 项目上获得了原生广告。

public class Event_Discover_Combined_Adapter extends RecyclerView.Adapter<Event_Discover_Combined_Adapter.MyViewHolder> {

private Context context;
private ArrayList<Event_Discover_Combined_Details> beanList;
private int itemCount;

public Event_Discover_Combined_Adapter(Context context, ArrayList<Event_Discover_Combined_Details> beanList, int itemCount) {
    this.context = context;
    this.beanList = beanList;
    this.imageLoader = ImageLoader.getInstance();
    this.itemCount = itemCount;

}

//int viewType ;
@Override
public int getItemViewType(int position) {

    if (position % 3 == 0) {
        return R.layout.native_ad_frame_layout;
    } else {
        if (beanList.get(position).getType() == ConstantUrl.TYPE_EVENT) {
            return R.layout.events_item;
        } else {
            return R.layout.discover_item;
        }
    }
}

@Override
public Event_Discover_Combined_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(context).inflate(viewType, parent, false);
    return new MyViewHolder(itemView);
}

public static ImageLoader imageLoader;
// public static ImageLoader imageLoader2;

@Override
public void onBindViewHolder(final Event_Discover_Combined_Adapter.MyViewHolder holder, int position) {
    Event_Discover_Combined_Details bean = beanList.get(position);

    if (position % 3 == 0) {
        //Ad View
        //For Native Ads
        AdLoader.Builder builder = new AdLoader.Builder(context, context.getResources().getString(R.string.ADMOB_AD_UNIT_ID));

        //if (requestContentAds) {
        builder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
            @Override
            public void onContentAdLoaded(NativeContentAd ad) {
                NativeContentAdView adView = (NativeContentAdView) getLayoutInflater()
                        .inflate(R.layout.native_ad_content, null);
                populateContentAdView(ad, adView);
                holder.frameLayout.removeAllViews();
                holder.frameLayout.addView(adView);
            }
        });
        //}

        AdLoader adLoader = builder.withAdListener(new AdListener() {
            @Override
            public void onAdFailedToLoad(int errorCode) {
                Toast.makeText(context, "Failed to load native ad: "
                        + errorCode, Toast.LENGTH_SHORT).show();
            }
        }).build();
        adLoader.loadAd(new AdRequest.Builder().build());

    } else {
        //Normal View

        AppLog.Log("combine", "beanlist" + bean.getType());

        if (bean.getType() == ConstantUrl.TYPE_DISCOVER) {
            if (!bean.getDiscoverDetails().getIcon().equals(""))
                imageLoader.displayImage(bean.getDiscoverDetails().getIcon(),
                        holder.iv_discover_icon);

            holder.tv_author_name.setText(bean.getDiscoverDetails().getAuthor());
            holder.item_discover_descrip.setText(bean.getDiscoverDetails().getDescription());
            holder.tv_discover_heading.setText(bean.getDiscoverDetails().getTitle());

        } else if (bean.getType() == ConstantUrl.TYPE_EVENT) {
            if (!bean.getEventDetails().getIcon().equals(""))
                imageLoader.displayImage(bean.getEventDetails().getIcon(),
                        holder.event_icon);

  /*  holder.item_event_date.setText(bean.getStartDate());
    holder.item_event_time.setText(bean.getStartDate());*/
            try {
                Date date1 = AppUtils.getFormatedDate(bean.getEventDetails().getStartDate());

                String event_time = (String) android.text.format.DateFormat.format("EEEE", date1) + " " + context.getString(R.string.at_text) + " " + new SimpleDateFormat("hh:mm aa").format(date1);

                // holder.item_event_date.setText((String) android.text.format.DateFormat.format("dd", date1));

                holder.item_event_date.setText(event_time);
                holder.item_event_time.setText(event_time);


            } catch (Exception e) {
                e.printStackTrace();
            }

            holder.item_event_heading.setText(bean.getEventDetails().getName());
            holder.item_event_descrip.setText(bean.getEventDetails().getDescription());

        }

    }

}

@Override
public int getItemCount() {
    return itemCount;
}

// int count = 0;

public void setItemCount(int count) {

    this.itemCount = count;

}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    //For Discover
    public ImageView iv_discover_icon;
    MyTextView tv_author_name;
    MyTextView item_discover_descrip;
    MyTextView_medium tv_discover_heading;

    //For Events
    public ImageView event_icon;
    public MyTextView_medium item_event_time;
    MyTextView item_event_date;
    MyTextView item_event_descrip;
    MyTextView_medium item_event_heading;

    //For Native Ads
    FrameLayout frameLayout;

    public MyViewHolder(final View itemView) {
        super(itemView);

        //For Discover
        iv_discover_icon = (ImageView) itemView.findViewById(R.id.iv_discover_icon);
        tv_author_name = (MyTextView) itemView.findViewById(R.id.tv_author_name);
        tv_discover_heading = (MyTextView_medium) itemView.findViewById(R.id.tv_discover_heading);
        item_discover_descrip = (MyTextView) itemView.findViewById(R.id.item_discover_descrip);
        itemView.setOnClickListener(this);

        //For Events
        event_icon = (ImageView) itemView.findViewById(R.id.event_icon);
        item_event_date = (MyTextView) itemView.findViewById(R.id.item_event_date);
        item_event_time = (MyTextView_medium) itemView.findViewById(R.id.item_event_time);
        item_event_heading = (MyTextView_medium) itemView.findViewById(R.id.item_event_heading);
        item_event_descrip = (MyTextView) itemView.findViewById(R.id.item_event_descrip);
        itemView.setOnClickListener(this);

        //For Native Ads
        frameLayout = (FrameLayout) itemView.findViewById(R.id.fl_adplaceholder);

    }

    @Override
    public void onClick(View v) {
        if ((beanList.get(getAdapterPosition()).getType()) == ConstantUrl.TYPE_EVENT) {
            Intent i = new Intent(context, Events.class);
            i.putExtra("event_details", beanList.get(getAdapterPosition()).getEventDetails());
            context.startActivity(i);
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(beanList.get(getAdapterPosition()).getDiscoverDetails().getUrl()));
            context.startActivity(intent);
        }
    }
}

private LayoutInflater getLayoutInflater() {
    AppLog.Log("classmate5", "inside_getLayoutInflater" + LayoutInflater.from(context));
    return LayoutInflater.from(context);
}

private void populateContentAdView(NativeContentAd nativeContentAd,
                                   NativeContentAdView adView) {
    AppLog.Log("classmate5", "inside_populateContentAdView");
    adView.setHeadlineView(adView.findViewById(R.id.contentad_headline));
    adView.setImageView(adView.findViewById(R.id.contentad_image));
    adView.setBodyView(adView.findViewById(R.id.contentad_body));
    adView.setCallToActionView(adView.findViewById(R.id.contentad_call_to_action));
    adView.setLogoView(adView.findViewById(R.id.contentad_logo));
    adView.setAdvertiserView(adView.findViewById(R.id.contentad_advertiser));

    // Some assets are guaranteed to be in every NativeContentAd.
    ((TextView) adView.getHeadlineView()).setText(nativeContentAd.getHeadline());
    ((TextView) adView.getBodyView()).setText(nativeContentAd.getBody());
    ((TextView) adView.getCallToActionView()).setText(nativeContentAd.getCallToAction());
    ((TextView) adView.getAdvertiserView()).setText(nativeContentAd.getAdvertiser());

    List<NativeAd.Image> images = nativeContentAd.getImages();

    if (images.size() > 0) {
        ((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable());
    }

    // Some aren't guaranteed, however, and should be checked.
    NativeAd.Image logoImage = nativeContentAd.getLogo();

    if (logoImage == null) {
        adView.getLogoView().setVisibility(View.INVISIBLE);
    } else {
        ((ImageView) adView.getLogoView()).setImageDrawable(logoImage.getDrawable());
        adView.getLogoView().setVisibility(View.VISIBLE);
    }

    // Assign native ad object to the native view.
    adView.setNativeAd(nativeContentAd);
}

}

我无法弄清楚要检查的位置以及如何在我的 gridview (recyclerview) 的项目之间添加原生广告。

我的发现项目的布局,我有一个框架布局。在这个框架布局中,我正在加载原生广告。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iv_discover_icon"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:scaleType="centerCrop"
            android:src="@drawable/events" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:layout_marginTop="2dp"
            android:background="@color/white"
            android:orientation="vertical">


            <com.erbauen.widgets.MyTextView_medium
                android:id="@+id/tv_discover_heading"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/item_event_detail"
                android:alpha="0.6"
                android:lines="2"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:text="Network over Coffee"
                android:textColor="@color/black"
                android:textSize="@dimen/events_item_date_time"

                />

            <com.erbauen.widgets.MyTextView
                android:id="@+id/tv_author_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/item_event_detail"
                android:layout_marginTop="2dp"
                android:alpha="0.6"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:singleLine="true"
                android:text="Network over Coffee"
                android:textColor="@color/black"
                android:textSize="@dimen/events_item_date_time"

                />

            <com.erbauen.widgets.MyTextView
                android:id="@+id/item_discover_descrip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/item_event_heading"
                android:layout_marginTop="1dp"
                android:fontFamily="Helvetica"
                android:maxLines="2"
                android:singleLine="false"
                android:text="Brief information about the event. Brief information about the event. Brief information about the event."
                android:textSize="@dimen/events_item_date_time"
                android:visibility="gone" />
        </LinearLayout>
    </LinearLayout>

    <!--<View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:alpha="0.5"
        android:background="@color/light_green">

    </View>-->

</android.support.v7.widget.CardView>

我从discover_item.xml 中删除了框架布局。现在如何隐藏和取消隐藏各自的布局?

4

1 回答 1

1

您需要从卡片布局中删除广告,或者您可以根据行号隐藏和显示您的视图。

您需要创建一个仅包含广告的不同卡片布局,并且需要在 onBindViewHolder 方法的每个第三个位置显示它

if (position % 3 == 0) {
    // inflate your ad view here or hide your normal native view here and show ad view
} else {
     // inflate your normal native view here or hide your ad view here and show normal native view
}
于 2016-08-18T13:50:28.233 回答