Android列表视图,每10个位置添加空项目以显示广告

时间:2017-03-01 16:59:16

标签: android listview admob

我需要每10个位置添加一个空项目,以便在列表视图中显示广告。我搜索了很多,但我找不到实现它的方法 我附上了我的ListAdapter,我想它可以从getCount()中添加,但我不能让它工作,看看你是否可以帮助我 谢谢!

public class MapListAdapter extends ArrayAdapter<Place> {
List<Place> places;

private LayoutInflater mInflater;
private Context mContext;
private String TAG_ADS = "ADS";

public MapListAdapter(Context context, List<Place> places) {
    super(context, 0, places);
    this.mContext = context;
    this.places = places;
    this.mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return places.size();
}

@Override
public Place getItem(int position) {
    return places.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final Place place = places.get(position);

    if (place == null){
        convertView = mInflater.inflate(R.layout.listview_ads, null);
        NativeExpressAdView adView = (NativeExpressAdView) convertView.findViewById(R.id.adView);

        AdRequest request = new AdRequest.Builder()
                .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxx")
                .build();

        adView.loadAd(request);
        convertView.setTag(TAG_ADS);
        return convertView;
    }


    ViewHolder holder;

    if(convertView == null || convertView.getTag().equals(TAG_ADS)){
        convertView = mInflater.inflate(R.layout.fragment_places_row, null);

        holder = new ViewHolder();
        holder.title= (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
        holder.date = (TextView) convertView.findViewById(R.id.userVideoDateTextView);
        holder.thumb =(ImageView) convertView.findViewById(R.id.userVideoThumbImageView);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

1 个答案:

答案 0 :(得分:0)

你可以使用模运算符,它是除法后的余数。 在getView函数中:

if(position % 10 == 0) {
//position is a multiple of 10
    convertView = mInflater.inflate(R.layout.listview_ads, null);
    NativeExpressAdView adView = (NativeExpressAdView) convertView.findViewById(R.id.adView);

    AdRequest request = new AdRequest.Builder()
            .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxx")
            .build();

    adView.loadAd(request);
    convertView.setTag(TAG_ADS);
    return convertView;
} else {
//position is not a multiple of 10
    if(convertView == null){
      convertView = mInflater.inflate(R.layout.fragment_places_row, null);

      holder = new ViewHolder();
      holder.title= (TextView) convertView.findViewById(R.id.userVideoTitleTextView);
      holder.date = (TextView) convertView.findViewById(R.id.userVideoDateTextView);
      holder.thumb =(ImageView) convertView.findViewById(R.id.userVideoThumbImageView);
      convertView.setTag(holder);

   } else {
       holder = (ViewHolder) convertView.getTag();
   }

   return convertView;
}

编辑:

仅用于在列表中添加空项目:

public MapListAdapter(Context context, List<Place> places) {
    super(context, 0, places);
    this.mContext = context;
    this.places = places;
    this.mInflater = LayoutInflater.from(context);

    for (int i =0; i < this.places.size(); i += 10) {
      this.place.add(i, null);
    }
}

或者您可以在将列表提供给适配器之前使用循环。

希望这有帮助。

抱歉我的英文。