作者:多米音乐_35946826 | 来源:互联网 | 2023-05-26 16:18
我正在尝试将我的应用程序从listview升级到recyclerview.当我使用listview时,我使用本教程在listview中嵌入了广告.
我无法在recyclerview中添加它.有关如何在Recyclerview中完成此操作的任何观点?
目前在我的列表视图中,加载广告的代码如下:
if ((position % k) == 0) {
if (convertView instanceof AdView) {
return convertView;
} else {
// Create a new AdView
AdView adView = new AdView(activity, AdSize.BANNER,
ADMOB_ID);
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 - (int) Math.ceil(position / k) - 1,
convertView, parent);
}
它应该是这样的:
更新:从谷歌引用此视频,它给出了完整的解释
1> Cigogne Eve..:
在适配器中,首先需要覆盖getItemViewType,例如:
@Override
public int getItemViewType(int position)
{
if (position % 5 == 0)
return AD_TYPE;
return CONTENT_TYPE;
}
然后在onCreateViewHolder中,根据类型膨胀不同的视图.像这样的东西:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
View v = null;
if (viewType == AD_TYPE)
{
v = new AdView(activity, AdSize.BANNER, ADMOB_ID);
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);
v.setLayoutParams(params);
AdRequest adRequest = new AdRequest.Builder().build();
if (adRequest != null && v != null){
v.loadAd(adRequest);
}
}
else
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_layout, viewGroup, false);
RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(v);
return viewHolder;
}
(位置%5 == 0)更改视图类型会跳过该位置的实际项目吗?
不要忘记相应地更改getItemCount():'return items_count +(items_count%5)'
@nbtk应该是item_count/5而不是"%"
在我的脑海中只有一个问题,是否可以在回收者视图中显示admob广告?我的意思是,每隔5或10个项目间隔.它是否打破任何善意政策?
这实际上是跳过项目.我测试了它,确实如此.另外,你可以在getItemViewType()获得IndexOutOfBoundsException
2> Volodymyr Ku..:
如何添加AD列表项,不要跳过内容项,不要将空项插入内容列表?
private static final int LIST_AD_DELTA = 3;
private static final int COnTENT= 0;
private static final int AD = 1;
@Override
public int getItemViewType(int position) {
if (position > 0 && position % LIST_AD_DELTA == 0) {
return AD;
}
return CONTENT;
}
@Override
public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == CONTENT) {
return new ContentRecyclerHolder(parent, layoutId) {
@Override
protected void onCardClick(CardView card) {
fragmentManager.showPagerFragmentWithTransition(card.getContext(), getRealPosition(getAdapterPosition()));
}
};
} else {
return new AdRecyclerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_ad, parent, false));
}
}
@Override
public int getItemCount() {
int additiOnalContent= 0;
if (data.size() > 0 && LIST_AD_DELTA > 0 && data.size() > LIST_AD_DELTA) {
additiOnalContent= data.size() / LIST_AD_DELTA;
}
return data.size() + additionalContent;
}
@Override
public void onBindViewHolder(BaseRecyclerHolder baseHolder, int position) {
if (getItemViewType(position) == CONTENT) {
ContentRecyclerHolder holder = (ContentRecyclerHolder) baseHolder;
Content cOntent= data.get(getRealPosition(position));
} else {
AdRecyclerHolder holder = (AdRecyclerHolder) baseHolder;
AdRequest adRequest = new AdRequest.Builder().build();
if (adRequest != null && holder.adView != null){
holder.adView.loadAd(adRequest);
}
}
}
private int getRealPosition(int position) {
if (LIST_AD_DELTA == 0) {
return position;
} else {
return position - position / LIST_AD_DELTA;
}
}
在getItemViewType中获取IndexOutOfBounds异常
3> n1m1..:
在ArrayList中,将空值添加到要显示广告的位置
for(int i=0;i
然后在getItemViewType()中,
@Override
public int getItemViewType(int position) {
if(qlist.get(position)==null)
return AD_TYPE;
return CONTENT_TYPE;
}
然后,
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = null;
ViewHolder vh=null;
if (viewType == AD_TYPE)
{
v=inflater.inflate(R.layout.adview_item, parent, false);
vh=new AdviewHolder(v);
}
else {
v = inflater.inflate(R.layout.cardview_items, parent, false);
vh = new ContentViewHolder(v);
}
return vh;
}
这不会跳过任何项目,但会将广告插入所需的位置.