gpt4 book ai didi

java - 每隔 X 行将 Admob 横幅添加到 ListView

转载 作者:行者123 更新时间:2023-11-29 04:08:18 27 4
gpt4 key购买 nike

我知道这个问题已经被问过多次了。我已经在 stackover flow 上浏览了很多票,但我似乎找不到任何我能清楚理解的东西。我已经能够让 Admob 横幅出现在我的应用程序中,但我无法弄清楚如何将它放入我的 ListView 中。非常感谢我的指点和/或帮助。

Putting an AdMob native ad in a listView

这是我希望横幅广告出现的适配器。

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
League league = leaguesList.get(position);



if (position == 0) {
// Create and return Ad View
//int id = league.getId();
//String leagueId = String.valueOf(id);
//holder.id.setText(leagueId);
holder.name.setText("BANNER GOES HERE");
} else {
// Create and return a normal View
int id = league.getId();
String leagueId = String.valueOf(id);
holder.id.setText(leagueId);
holder.name.setText(league.getName());
}



//Formatting And Displaying Timestamp
holder.timestamp.setText(formatDate(league.getTimestamp()));
}

这是我的横幅广告代码

// Initialize the Mobile Ads SDK

//setContentView(R.layout.content_main);
MobileAds.initialize(this,
getString(R.string.admob_app_id));

// Find Banner ad
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
// Display Banner ad
mAdView.loadAd(adRequest);
}

我不确定如何将广告代码合并到 View 持有者中以使其正常工作。

再次感谢任何帮助。

联盟适配器代码(已修改)

    public class LeagueAdapter extends RecyclerView.Adapter<LeagueAdapter.MyViewHolder> {

private Context context;
private List<League> leaguesList;

public void notifyDatasetChanged(List<League> newleagueslist) {
leaguesList.clear();
leaguesList.addAll(newleagueslist);
super.notifyDataSetChanged();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
private TextView leagueAverage;
public TextView id;
public TextView timestamp;

public MyViewHolder(View view) {
super(view);
id = view.findViewById( R.id.tvLeagueId);
name = view.findViewById(R.id.tvSeriesName );
leagueAverage = view.findViewById(R.id.tvLeagueAverage);
timestamp = view.findViewById(R.id.timestamp);
}
}


public LeagueAdapter(Context context, List<League> leaguesList) {
this.context = context;
this.leaguesList = leaguesList;
}

public static class ViewHolderAdMob extends RecyclerView.ViewHolder {
public AdView mAdView;
public ViewHolderAdMob(View view) {
super(view);
mAdView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mAdView.loadAd(adRequest);
}
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//View itemView = LayoutInflater.from(parent.getContext())
// .inflate(R.layout.listview_league, parent, false);
RecyclerView.ViewHolder viewHolder;
viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch(viewType){
case 1:{
View v = inflater.inflate(R.layout.listview_league, parent, false);
viewHolder = new MyViewHolder(v);
break;
}
case 2:{
View v = inflater.inflate(R.layout.listview_league_admob, parent, false);
viewHolder = new ViewHolderAdMob(v);
break;
}
}
//return (MyViewHolder) itemView;

return new MyViewHolder(viewHolder);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//League league = leaguesList.get(position);

League league1 = leaguesList.get(holder.getAdapterPosition());

switch(holder.getItemViewType()){
case 1:{
MyViewHolder viewHolder = (MyViewHolder) holder;
viewHolder.name.setText(league1.getName());
viewHolder.timestamp.setText(formatDate(league1.getTimestamp()));
break;
}
case 2:{
break;
}
}

}

@Override
public int getItemCount() {
return leaguesList.size();
}

//Formatting TimeStamp to 'EEE MMM dd yyyy (HH:mm:ss)'
//Input : 2018-05-23 9:59:01
//Output : Wed May 23 2018 (9:59:01)
private String formatDate(String dateStr) {
try {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = fmt.parse(dateStr);
SimpleDateFormat fmtOut = new SimpleDateFormat("EEE MMM dd yyyy (HH:mm:ss)");
return fmtOut.format(date);
} catch (ParseException e) {

}

return "";
}
}

布局 XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:autofit="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:paddingBottom="@dimen/dimen_10"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/dimen_10"
android:focusable="true">

<me.grantland.widget.AutofitTextView
android:id="@+id/tvSeriesName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:singleLine="true"
android:text="@string/leagueValue"
android:textColor="?attr/colorText1"
android:textSize="24sp"
autofit:minTextSize="16sp" />

<TextView
android:id="@+id/tvLeagueAverage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/tvSeriesName"
android:layout_marginStart="0dp"
android:text="League Average: 300"
android:textColor="#000000"
android:textSize="18sp"
android:visibility="gone" />

<TextView
android:id="@+id/timestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvSeriesName"
android:layout_centerHorizontal="true"
android:text="Fri May 18 2018"
android:textColor="?attr/colorText1"
android:textSize="10sp"
android:visibility="gone" />

<TextView
android:id="@+id/tvLeagueId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/tvSeriesName"
android:layout_below="@+id/timestamp"
android:text="TextView"
android:textColor="?attr/colorText1"
android:visibility="gone" />

<!-- set Banner ad position in UI layout design -->
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/admob_banner_id" />

</RelativeLayout>

最佳答案

我的想法是覆盖 getItemViewType 并返回另一种类型对于每个偶数位置 (%)。

onCreateViewHolder 中使用 viewType 来确定您要使用哪个 viewHolder。您应该有两个具有不同布局的 viewHolder(或一个具有设置的可见性)。一个 viewHolder 将是您列表中的一个普通项目。第二个将容纳 adView。

祝你好运

关于java - 每隔 X 行将 Admob 横幅添加到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56782754/

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