2017-08-08 61 views
0

如何在列表視圖中的每10條記錄之後顯示廣告橫幅?如何在列表視圖中管理廣告橫幅內的項目位置

我可以在列表視圖項中設置廣告橫幅,但我無法管理列表視圖項的位置。 當我點擊listview項目,每次我得到下一個項目的位置。前者爲 。 我在列表視圖中和每10條記錄廣告後加載20條記錄。應該顯示橫幅。

+1

您可能需要使用自定義適配器,並攻入像getView它接受行號,返回該視圖中,在那裏你必須做你自己的數學,讓您在8/10個項目的廣告,而不會錯過這些項目的方法,即轉移/管理他們順從 –

+1

:)謝謝你回答我的關注 –

+1

請參閱此鏈接https://stackoverflow.com/a/27972680/8143436 –

回答

0
Follow this code 

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    if (position == 0) 
    { 
     if (convertView instanceof AdView) 
     { 
      return convertView; 
     } 
     else 
     { 
      AdView adView = new AdView(activity, AdSize.BANNER, ADMOB_PUBLISHER_ID); 
      // Disable focus for sub-views of the AdView to avoid problems with 
      // trackpad navigation of the list. 
      for (int i = 0; i < adView.getChildCount(); i++) 
      { 
       adView.getChildAt(i).setFocusable(false); 
      } 
      adView.setFocusable(false); 
      // Default layout params have to be converted to ListView compatible 
      // params otherwise there will be a ClassCastException. 
      float density = activity.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); 
      adView.loadAd(new AdRequest()); 
      return adView; 
     } 
    } 
    else 
    { 
     return delegate.getView(position - 1, convertView, parent); 
    } 
} 

@Override 
public int getViewTypeCount() 
{ 
    return delegate.getViewTypeCount() + 1; 
} 

@Override 
public int getItemViewType(int position) 
{ 
    return position == 0 ? delegate.getViewTypeCount() 
         : delegate.getItemViewType(position - 1); 
}