繁体   English   中英

每隔X行将Admob横幅添加到列表视图

[英]Adding an Admob banner to a listview every X rows

我知道这个问题已被多次询问过。 我在堆栈流程中经历了很多门票,我似乎找不到任何我能清楚理解的东西。 我已经能够在我的应用程序中显示Admob Banner,但我无法弄清楚如何将其放入我的列表视图中。 我非常感谢我的指示和/或帮助。

将AdMob原生广告放入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);
    }

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

再次感谢任何协助。

联盟适配器代码(修改)

    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>

这可以通过许多方式实现。 最简单的方法是在列表项布局中添加adview,并使其在onBindViewHolder中可见或不可见。 您可以向League类添加一个额外的整数变量,例如'viewType',以确定要显示的实际行项目或要显示的adview。 在将列表传递给适配器之前,迭代列表并按需要的时间间隔添加广告项。 然后,您只需检查viewType并显示或隐藏广告视图和实际行项目内容即可。

内部活动/片段(在将列表数据发送到适配器构造函数之前)

int interval = 0;
for(League model : oldList){
   if(interval == 5){
      list.add(new League(1, param, param));
      interval = 0;
   }else{
      model.setType(0);
      interval ++;
   }
   list.add(model);
}

在上面的代码之后设置适配器。

您不必在onBindViewHolder中编写迭代代码。 你的onBindViewHolder必须如下所示

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

    if(league.getType() == 1){
        //Adview. Shoe Adview and hide item viewgroup / items
    }else{
        //Row item content. Hide Adview and show item viewgroup / items
    }
}

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

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

祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM