Firebase recyclerview 活动中的原生广告
Posted
技术标签:
【中文标题】Firebase recyclerview 活动中的原生广告【英文标题】:Native ads in a firebase recylerview activity 【发布时间】:2021-10-07 14:05:24 【问题描述】:这是用于在 firebase 数据库的 recyclerview 中显示项目的 Activity。在活动中,我还放置了我的原生广告代码,但我的广告没有在每 5 个项目后正确显示。在 recyclerview 中每次卡片浏览后,广告总是会在几秒钟内一一加载。
请帮助我解决问题,我希望我的广告能够使用下面显示的代码正确显示在我的回收站视图中。
public class HOME_RV_BUS_ACTIVITY extends AppCompatActivity
EditText inputSearch;
RecyclerView recyclerView;
private Toolbar toolbar;
FirebaseRecyclerOptions<BusModel> options;
FirebaseRecyclerAdapter<BusModel,BusViewHolder> adapter;
DatabaseReference Dataref;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // To remove status Bar
setContentView(R.layout.activity_home__rv__bus__activity);
toolbar = (Toolbar) findViewById(R.id.myToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //To show back arrow
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_black_keyboard_arrow);//customize my own back arrow color
getSupportActionBar().setTitle(html.fromHtml("<font color=\"#000000\">" + "List Of InterCity Buses" +"</font>")); //change title on your own choice and also change it's color also
Dataref = FirebaseDatabase.getInstance().getReference().child("Bus");
inputSearch = findViewById(R.id.inputSearch);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setHasFixedSize(true);
LoadData("");
inputSearch.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
@Override
public void afterTextChanged(Editable s)
if (s.toString()!=null)
LoadData(s.toString());
else
LoadData("");
);
//onCreate end
private void LoadData(String data)
Query query=Dataref.orderByChild("BusName").startAt(data).endAt(data+"\uf8ff");
options=new FirebaseRecyclerOptions.Builder<BusModel>().setQuery(query,BusModel.class).build();
adapter=new FirebaseRecyclerAdapter<BusModel, BusViewHolder>(options)
@Override
protected void onBindViewHolder(@NonNull BusViewHolder holder, final int position, @NonNull BusModel model)
/////////////////////NativeAd////////////////////////////
/* MobileAds.initialize(this, new OnInitializationCompleteListener()
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) */
if (position % 5 == 0)
AdLoader.Builder builder = new AdLoader.Builder(HOME_RV_BUS_ACTIVITY.this, "ca-app-pub-3940256099942544/2247696110");
builder.forNativeAd(new NativeAd.OnNativeAdLoadedListener()
@Override
public void onNativeAdLoaded(NativeAd nativeAd)
Log.d("TAG", "OnNativeAdLoaded");
if (isDestroyed())
nativeAd.destroy();
return;
NativeTemplateStyle styles = new
NativeTemplateStyle.Builder().build();
TemplateView template = findViewById(R.id.my_template);
template.setStyles(styles);
template.setVisibility(View.VISIBLE);
template.setNativeAd(nativeAd);
);
builder.withAdListener(new AdListener()
@Override
public void onAdFailedToLoad(LoadAdError adError)
// Handle the failure by logging, altering the UI, and so on.
);
builder.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build());
AdLoader adLoader = builder
.build();
adLoader.loadAd(new AdRequest.Builder().build());
holder.templateView.setVisibility(View.VISIBLE);
/*
);*/
///////////////////////Native Ad End////////////////////
holder.textView.setText(model.getBusName());
Picasso.get().load(model.getImageUrl()).into(holder.imageView);
holder.v.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(HOME_RV_BUS_ACTIVITY.this, BusPicViewActivity.class);
intent.putExtra("BusKey", getRef(position).getKey());
startActivity(intent);
);
@NonNull
@Override
public BusViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view_bus,parent,false);
return new BusViewHolder(v);
;
adapter.startListening();
recyclerView.setAdapter(adapter);
这是模型类
public class BusModel
private String BusName;
private String ImageUrl;
public BusModel(String busName, String imageUrl)
BusName = busName;
ImageUrl = imageUrl;
public BusModel()
public String getBusName()
return BusName;
public void setBusName(String busName)
BusName = busName;
public String getImageUrl()
return ImageUrl;
public void setImageUrl(String imageUrl)
ImageUrl = imageUrl;
这是我也放置广告模板视图的 ViewHolder
public class BusViewHolder extends RecyclerView.ViewHolder
ImageView imageView;
TextView textView;
View v;
TemplateView templateView;
public BusViewHolder(@NonNull View itemView)
super(itemView);
imageView=itemView.findViewById(R.id.image_single_view);
textView=itemView.findViewById(R.id.textView_single_view);
templateView = itemView.findViewById(R.id.my_template);
v = itemView;
【问题讨论】:
【参考方案1】:myviewholde.adlayout.setVisibility(View.GONE);
if (i % 5 == 0)
myviewholde.adlayout.setVisibility(View.VISIBLE);
AdLoader.Builder builder = new AdLoader.Builder(myviewholde.button_down.getContext(), "ca-app-pub-3940256099942544/2247696110");
builder.forNativeAd(new NativeAd.OnNativeAdLoadedListener()
@Override
public void onNativeAdLoaded(NativeAd nativeAd)
Log.d("TAG", "OnNativeAdLoaded");
NativeTemplateStyle styles = new
NativeTemplateStyle.Builder().build();
TemplateView template =myviewholde.templateView.findViewById(R.id.id_ad_template_view);
template.setStyles(styles);
template.setVisibility(View.VISIBLE);
template.setNativeAd(nativeAd);
);
builder.withAdListener(new AdListener()
@Override
public void onAdFailedToLoad(LoadAdError adError)
// Handle the failure by logging, altering the UI, and so on.
);
builder.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build());
AdLoader adLoader = builder
.build();
adLoader.loadAd(new AdRequest.Builder().build());
myviewholde.templateView.setVisibility(View.VISIBLE);
1.将您的广告视图放在布局中并使其不可见 2.你需要学习recyleview的概念recyleview不会创建新的视图 这就是为什么你会一一看到你的负载
【讨论】:
以上是关于Firebase recyclerview 活动中的原生广告的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView没有被来自firebase的项目填充,只是一个空白的活动页面。
单击 RecyclerView 后使用 Firebase 实时数据库填充详细信息活动
Firebase UI RecyclerView onClick
在Recyclerview Android中从Firebase检索日期