如何在RecyclerView之间随机放置广告? [英] How to place ads randomly in between of RecyclerView?
本文介绍了如何在RecyclerView之间随机放置广告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的应用中使用了 FastAdapter ,我想将广告随机放置在 RecyclerView
。例如,例如3个 RecyclerView
项目之后的广告,然后是4之后的项目,然后是2之后的项目,依此类推。
I'm using FastAdapter in my app and I want to place ads randomly in between of RecyclerView
. For ex - like an ad after 3 RecyclerView
items then after 4, then after 2 and so on and so forth.
这是我使用FastAdapter的方式:
This is how I'm using FastAdapter:
FastItemAdapter<HRequest> fastItemAdapter = new FastItemAdapter<>();
fastItemAdapter.withSelectable(true);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
HRequest hRequest = new HRequest(imageUID);
fastItemAdapter.add(helpRequest);
recyclerView.setAdapter(fastItemAdapter);
这是 HRequest.java
文件的代码:
public class HRequest extends AbstractItem<HRequest, HRequest.ViewHolder> {
public String imageURL;
public HRequest() {
}
public HRequest(String imageURL) {
this.imageURL = imageURL;
}
// Fast Adapter methods
@Override
public int getType() {
return R.id.recycler_view;
}
@Override
public int getLayoutRes() {
return R.layout.h_request_list_row;
}
@Override
public void bindView(ViewHolder holder) {
super.bindView(holder);
holder.imageURL.setText(imageURL);
}
// Manually create the ViewHolder class
protected static class ViewHolder extends RecyclerView.ViewHolder {
TextView imageURL;
public ViewHolder(View itemView) {
super(itemView);
imageURL = (TextView)itemView.findViewById(R.id.imageURL);
if (!imageURL.getText().toString().isEmpty()) {
if (imageURL.getText().toString().startsWith("https://firebasestorage.googleapis.com/") || imageURL.getText().toString().startsWith("content://")) {
Picasso.with(itemView.getContext())
.load(imageURL.getText().toString())
.into(homelessImage);
} else {
Toast.makeText(itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
}
}
}
}
如何实现我想要的?
推荐答案
您的代码不足以让我完全掌握图片,但这是我想出的。让我知道这是否可行,或者我缺少什么。
Your code was not enough for me to fully get the picture but this is what I came up with. Let me know if this works or there's something I am missing.
public class HRequest extends AbstractItem<HRequest, HRequest.ViewHolder> {
public static int count = 0;
public static int random = 0;
public String imageURL;
public HRequest() {
}
public HRequest(String imageURL) {
this.imageURL = imageURL;
Random rand = new Random();
random = random.nextInt(4);
}
// Fast Adapter methods
@Override
public int getType() {
return R.id.recycler_view;
}
@Override
public int getLayoutRes() {
return R.layout.h_request_list_row;
}
@Override
public void bindView(ViewHolder holder) {
super.bindView(holder);
holder.imageURL.setText(imageURL);
if(count >= random ){
// Load Your Ad
random = random.nextInt(4); // Reset the counter to random integer
count = 0;
}else{
count++;
if (!imageURL.getText().toString().isEmpty()) {
if(imageURL.getText().toString().startsWith("https://firebasestorage.googleapis.com/") || imageURL.getText().toString().startsWith("content://")) {
Picasso.with(itemView.getContext())
.load(imageURL.getText().toString())
.into(homelessImage);
} else {
Toast.makeText(itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
}
}
}
// Manually create the ViewHolder class
protected static class ViewHolder extends RecyclerView.ViewHolder {
TextView imageURL;
public ViewHolder(View itemView) {
super(itemView);
imageURL = (TextView)itemView.findViewById(R.id.imageURL);
}
}
这篇关于如何在RecyclerView之间随机放置广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文