问题
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.
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);
Here's HRequest.java
file's code:
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();
}
}
}
}
How can I achieve what I want?
回答1:
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);
}
}
来源:https://stackoverflow.com/questions/38023511/how-to-place-ads-randomly-in-between-of-recyclerview