我需要在recyclerview之间放置广告,我尝试了一种方法,它运行良好,广告加载但问题是广告在列表视图上重叠。有人请你帮我提前谢谢
以下是Adapter.class的代码
public class NewsAdapter extends RecyclerView.Adapter<ViewHolder> {
private static final int CONTENT_TYPE = 0;
private static final int AD_TYPE = 1;
private int[] viewTypes;
int Holderid;
private List<Newslist> newslists;
private Context context;
public NewsAdapter(List<Newslist> newslists, Context context) {
super();
//Getting all the news updates
this.newslists = newslists;
this.context=context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
ViewHolder holder = null;
if (viewType == CONTENT_TYPE) {
View newsView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_list, parent, false);
holder=new ItemViewHolder(newsView);
} else {
View adView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_ads, parent, false);
holder=new AdViewHolder(adView);
}
return holder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
if (position % 6 == 3) {
AdViewHolder holder = (AdViewHolder) viewHolder;
//Load the Ad
AdRequest request = new AdRequest.Builder()
.build();
holder.adView.loadAd(request);
} else {
ItemViewHolder holder = (ItemViewHolder) viewHolder;
Newslist newslist = newslists.get(holder.getAdapterPosition());
holder.message.setText(newslist.getMessage());
//Image loading using singleton class
ImageLoader imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
holder.imageView.setImageUrl(newslist.getImageurl(), imageLoader);
holder.imageView.setDrawingCacheBackgroundColor(R.mipmap.ic_launcher);
holder.time.setText(newslist.getTime());
}
}
@Override
public int getItemCount() {
return newslists.size();
}
@Override
public int getItemViewType(int position) {
if (position % 6 ==3)
return AD_TYPE;
return CONTENT_TYPE;
}
// return newslists.get(position).getViewType();
}
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View v) {
super(v);
}
}
class ItemViewHolder extends ViewHolder {
TextView message;
NetworkImageView imageView;
TextView time;
RelativeLayout relativeLayout;
ItemViewHolder(View itemView) {
super(itemView);
message = (TextView) itemView.findViewById(R.id.headline);
time = (TextView) itemView.findViewById(R.id.time);
imageView = (NetworkImageView) itemView.findViewById(R.id.networkImageView2);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
}
}
class AdViewHolder extends ViewHolder {
NativeExpressAdView adView;
AdViewHolder(View v) {
super(v);
adView = (NativeExpressAdView) v.findViewById(R.id.nativeAdView);
}
}
答案 0 :(得分:2)
public class NewsAdapter extends RecyclerView.Adapter<ViewHolder> {
private static final int CONTENT_TYPE = 0;
private static final int AD_TYPE = 1;
private int[] viewTypes;
int Holderid;
private List<Newslist> newslists;
private Context context;
public NewsAdapter(List<Newslist> newslists, Context context) {
super();
//Getting all the news updates
this.newslists = newslists;
this.context=context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
ViewHolder holder = null;
if (viewType == CONTENT_TYPE) {
View newsView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_list, parent, false);
holder=new ItemViewHolder(newsView);
} else if(viewType == AD_TYPE){
View adView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_ads, parent, false);
holder=new AdViewHolder(adView);
}
return holder;
}
@Override
public void onBindViewHolder(final NewsAdapter.ItemViewHolder holder, final int position) {
if(holder.HOLDER == 1){
}else {
final int pos=position-Math.round(position / 6);
final Newslist objnewslist=newslists.get(pos);
holder.message.setText(objnewslist.getMessage());
//Image loading using singleton class
ImageLoader imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
holder.imageView.setImageUrl(objnewslist.getImageurl(), imageLoader);
holder.imageView.setDrawingCacheBackgroundColor(R.mipmap.ic_launcher);
holder.time.setText(objnewslist.getTime());
}
}
@Override
public int getItemCount() {
int size = newslists.size();
if(newslists.size() > 0) {
return newslists.size() + Math.round(newslists.size()/6);
}
return newslists.size();
}
@Override
public int getItemViewType(int position) {
if ((position+1)%6==0){
return AD_TYPE;
}
return CONTENT_TYPE;
}
// return newslists.get(position).getViewType();
}
class ItemViewHolder extends RecyclerView.ViewHolder {
TextView message;
NetworkImageView imageView;
TextView time;
RelativeLayout relativeLayout;
int HOLDER;
AdView madview;
ItemViewHolder(View itemView , int viewType) {
super(itemView);
if (viewType == AD_TYPE) {
madview = (AdView) itemView.findViewById(R.id.adView);
AdRequest adreq = new AdRequest.Builder().build();
madview.loadAd(adreq);
HOLDER = AD_TYPE;
}else {
message = (TextView) itemView.findViewById(R.id.headline);
time = (TextView) itemView.findViewById(R.id.time);
imageView = (NetworkImageView) itemView.findViewById(R.id.networkImageView2);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
HOLDER = CONTENT_TYPE;
}
}
}
检查上面的代码是否正常。
答案 1 :(得分:0)
当您致电ItemViewHolder
时,您应该输入viewType
值,而getItemCount()应该返回newslists.size()+ Math.round(newslists.size()/ (6 - 1)强>);