|
沙发
楼主 |
发表于 2018-9-3 10:16:13
|
只看该作者
- /**
- *
- */
- package com.figo.study;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
-
- import com.figo.study.bitmapcache.core.AsynImageLoader.LoadingListener;
- import com.figo.study.bitmapcache.core.AsynImageLoader.OnLoadImageCallBackListener;
- import com.facebook.ads.Ad;
- import com.facebook.ads.AdError;
- import com.facebook.ads.AdListener;
- import com.facebook.ads.NativeAd;
- import com.facebook.ads.NativeAdsManager;
- import com.facebook.ads.NativeAdsManager.Listener;
- import com.figo.study.utils.CommonUtils;
-
- /**
- * @author figo
- *
- */
- public class FbAdvertisementActivity extends Activity {
- private String tag = "FbAdvertisementActivity";
- private ImageView imgLogo;
- private TextView tvTitle, tvContent;
- private LinearLayout layout_ad;
- private ListView lvFbAds;
- List<NativeAd> nativeAds;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_facebookad);
- layout_ad = (LinearLayout) findViewById(R.id.layout_ad);
- imgLogo = (ImageView) findViewById(R.id.img_logo);
- tvTitle = (TextView) findViewById(R.id.tv_title);
- tvContent = (TextView) findViewById(R.id.tv_content);
- lvFbAds = (ListView) findViewById(R.id.lv_fbads);
- loadAd("1542807559327931_1609143346027685");
- }
-
- private void loadAd(String adId) {
- //加载多条广告
- final List<NativeAd> nativeAds = new ArrayList<NativeAd>();
- final NativeAdsManager nam = new NativeAdsManager(FbAdvertisementActivity.this, "1542807559327931_1609143346027685", 10);
- nam.setListener(new Listener() {
-
- @Override
- public void onAdsLoaded() {
- int count = nam.getUniqueNativeAdCount();
- while (count > 0) {
- nativeAds.add(nam.nextNativeAd());
- count--;
- }
- FbAdAdapter fba = new FbAdAdapter(nativeAds, FbAdvertisementActivity.this);
- lvFbAds.setAdapter(fba);
- }
-
- @Override
- public void onAdError(AdError paramAdError) {
- Log.e(tag, "FbNotificationNativeAd:onError:" + paramAdError.getErrorCode() + ", " + paramAdError.getErrorMessage());
- }
- });
- nam.loadAds();
- //加载单条广告
- final NativeAd nativeAd = new NativeAd(FbAdvertisementActivity.this, adId);
-
- nativeAd.setAdListener(new AdListener() {
-
- @Override
- public void onError(Ad ad, AdError error) {
- Log.e(tag, "FbNotificationNativeAd:onError:" + error.getErrorCode() + ", " + error.getErrorMessage());
-
- }
-
- @Override
- public void onAdLoaded(final Ad ad) {
- if (ad != nativeAd)
- return;
-
- if (nativeAd.getAdIcon() == null || nativeAd.getAdIcon().getUrl() == null)
- return;
- tvTitle.setText(nativeAd.getAdTitle());
- tvContent.setText(nativeAd.getAdSubtitle());
- //通过lrucache加载图片到内存
- MainApplication.shared().asynImageLoader.getImage(nativeAd.getAdIcon().getUrl(), 200, new OnLoadImageCallBackListener() {
-
- @Override
- public void onSuccess(String key, BitmapDrawable bitmap) {
-
- imgLogo.setImageDrawable(bitmap);
- }
-
- @Override
- public void onFailure(String key, int errorType) {
-
- }
- });
- nativeAd.registerViewForInteraction(layout_ad);
-
- }
-
- @Override
- public void onAdClicked(Ad ad) {
-
- }
- });
- nativeAd.loadAd();
- }
-
- class FbAdAdapter extends BaseAdapter {
- List<NativeAd> nativeAds;
- Context mContext;
-
- public FbAdAdapter(List<NativeAd> fbNativeAds, Context context) {
- nativeAds = fbNativeAds;
- mContext = context;
- }
-
- @Override
- public int getCount() {
- return nativeAds.size();
- }
-
- @Override
- public Object getItem(int position) {
- return nativeAds.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder viewHolder = null;
- if (convertView == null) {
- convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_facebookad, parent, false);
- viewHolder = new ViewHolder();
-
- viewHolder.imgLogo = (ImageView) convertView.findViewById(R.id.img_logo);
- viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
- viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
-
- convertView.setTag(viewHolder);
- } else {
- viewHolder = (ViewHolder) convertView.getTag();
- }
-
- // 将数据源中的数据填充到每一行中对应的控件
- MainApplication.shared().asynImageLoader.loadImageInContainer(nativeAds.get(position).getAdIcon().getUrl(), viewHolder.imgLogo, CommonUtils.dip2px(FbAdvertisementActivity.this, 72),
- getResources().getDrawable(R.drawable.ic_launcher), getResources().getDrawable(R.drawable.ic_launcher), new LoadingListener() {
-
- @Override
- public void onLoadStart() {
-
- }
-
- @Override
- public void onLoadInProgress(int progress) {
-
- }
-
- @Override
- public void onLoadEnd(boolean isSuccess) {
- notifyDataSetChanged();
- }
- });
- viewHolder.tvTitle.setText(nativeAds.get(position).getAdTitle());
- viewHolder.tvContent.setText(nativeAds.get(position).getAdSubtitle());
- nativeAds.get(position).registerViewForInteraction(convertView);
- return convertView;
- }
-
- }
-
- class ViewHolder {
- ImageView imgLogo;
- TextView tvTitle;
- TextView tvContent;
- }
- }
复制代码 |
|