我想在纵向模式中的特定位置查看我的列表视图中的Google广告。
在点击广告和返回应用时会导致错误。
有时当没有错误发生时,广告视图高度增加,滚动列表视图时广告会浮动。
public class ListAdaptor extends BaseAdapter {
private Context context;
AdView adView = null;
private ArrayList<String> titleList, descriptionList, pubDateList,
thumbnailList;
// int count = 2;
// private Context context;
private int listCount = 15;
//private final float mDensity;
int imageSize = 50;
LayoutInflater inflater;
int pageNumber = 0;
public ListAdaptor(Context c, ArrayList<String> titleList,
ArrayList<String> descriptionList, ArrayList<String> pubDateList,
ArrayList<String> thumbnailList, int pageNumber) {
context = c;
this.titleList = titleList;
this.descriptionList = descriptionList;
this.pubDateList = pubDateList;
this.thumbnailList = thumbnailList;
this.pageNumber = pageNumber;
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
// Log.d("imageSize "+display.getWidth(),"=="+imageSize);
imageSize = size.x / 3;
// Log.d("imageSize "+display.getHeight(),"="+imageSize);
//mDensity = c.getResources().getDisplayMetrics().density;
inflater = LayoutInflater.from(c);
// for (int i = 2; i < titleList.size(); i = i + 6) {
// titleList.add(i, "null");
// descriptionList.add(i, "");
// thumbnailList.add(i, "");
// pubDateList.add(i, "");
// i++;
// }
listCount = titleList.size();
}
@Override
public int getCount() {
return listCount;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("deprecation")
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (titleList.get(position).equalsIgnoreCase("null")) {
if (convertView == null || !(convertView instanceof AdView))
{
// Create a new AdView
if(adView == null)
{
try{
// Create a banner ad. The ad size and ad unit ID must be set before calling loadAd.
adView = new AdView(context.getApplicationContext());
adView.setAdSize(com.google.android.gms.ads.AdSize.BANNER);
adView.setAdUnitId(context.getString(R.string.banner_ad_unit_id));
// Convert the default layout parameters so that they play nice
// with
// ListView.
float density = context.getResources().getDisplayMetrics().density;
int height = Math.round(AdSize.BANNER.getHeight() * density);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(
AbsListView.LayoutParams.FILL_PARENT, height);
adView.setLayoutParams(params);
AdRequest adRequest = new AdRequest.Builder().
addTestDevice(Secure.getString(context.getContentResolver()
,Secure.ANDROID_ID)).build();
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
@Override
public void onAdOpened() {
// Save app state before going to the ad overlay.
}
});
}catch(Exception e){e.printStackTrace();}
}
return adView;
} else {
// Don’t instantiate new AdView, reuse old one
return convertView;
}
} else {
if (convertView == null || convertView instanceof AdView) {
view = inflater.inflate(R.layout.card_item, null);
// view.setLayoutParams(new
// ListView.LayoutParams(LayoutParams.MATCH_PARENT,(int) (90 *
// mDensity + 0.5f)));
} else {
view = convertView;
}
ImageView imgCardLitImage = (ImageView) view
.findViewById(R.id.imgCardLitImage);
TextView tvCardLitTitle = (TextView) view
.findViewById(R.id.tvCardLitTitle);
TextView tvCardListPubDate = (TextView) view
.findViewById(R.id.tvPubDate);
TextView tvCardLitDescription = (TextView) view
.findViewById(R.id.tvDescription);
ImageView imageVideoOverLay = (ImageView) view
.findViewById(R.id.imageVideoOverLay);
if (pageNumber == 1)
imageVideoOverLay.setVisibility(View.VISIBLE);
else
imageVideoOverLay.setVisibility(View.GONE);
/*
* If ImageList item is not available ,then display Description with
* Title
*/
if (thumbnailList.get(position) == null
&& descriptionList.get(position) != null) {
tvCardLitDescription.setVisibility(View.VISIBLE);
tvCardLitDescription.setText(descriptionList.get(position));
} else {
tvCardLitDescription.setVisibility(View.GONE);
}
tvCardLitTitle.setText(titleList.get(position));
tvCardListPubDate.setText(pubDateList.get(position));
// imgCardLitImage.getLayoutParams().width = (int) (370 * mDensity +
// 0.5f);
// imgCardLitImage.getLayoutParams().height = (int) (220 * mDensity
// + 0.5f);
/* Loading Card list Images */
try {
new ImageLoader(context, imageSize).DisplayImage(
thumbnailList.get(position), imgCardLitImage);
} catch (Exception e) {
e.printStackTrace();
}
}
return view;
}
}