gpt4 book ai didi

android - 如何在 RecyclerView 之间随机放置广告?

转载 作者:搜寻专家 更新时间:2023-11-01 09:46:06 24 4
gpt4 key购买 nike

我正在使用 FastAdapter在我的应用程序中,我想在 RecyclerView 之间随机放置广告。例如 - 就像在 3 个 RecyclerView 项目之后的广告,然后是 4 个项目之后,然后是 2 个项目之后,依此类推。

这就是我使用 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();
}

}
}

}

我怎样才能实现我想要的?

最佳答案

您的代码不足以让我完全理解图片,但这就是我想出的。让我知道这是否有效,或者我缺少什么。

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);

}

}

关于android - 如何在 RecyclerView 之间随机放置广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38023511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com