在此适配器中添加AdView

时间:2018-08-23 13:30:19

标签: android listview admob

我想在此适配器的第二个位置添加一个adview。为此我需要更改什么?最近两个小时我做了很多尝试。加载列表后可以插入吗?

这是我的适配器代码:

public class FeedsAdapter extends RecyclerView.Adapter<FeedsAdapter.MyViewHolder> {
    ArrayList<FeedItem> feedItems;
    MainActivity context;
    private static final String FILENAME = "prefsSettings";
    private static final String VAL_KEY7 = "news_format";

    public FeedsAdapter(MainActivity context, ArrayList<FeedItem> feedItems) {
        this.feedItems = feedItems;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        SharedPreferences sharedPrefs = context.getSharedPreferences(FILENAME, 0);
        String theme = sharedPrefs.getString(VAL_KEY7, "little");
        View view = null;
        if(theme.equals("big")) {
            view = LayoutInflater.from(context).inflate(R.layout.custum_row_news_item, parent, false);
        } else if (theme.equals("little")) {
            view = LayoutInflater.from(context).inflate(R.layout.custum_row_news_item_smal, parent, false);
        } else if (theme.equals("none")) {
            view = LayoutInflater.from(context).inflate(R.layout.custum_row_news_item_none, parent, false);
        }
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        SharedPreferences sharedPrefs = context.getSharedPreferences(FILENAME, 0);
        String theme = sharedPrefs.getString(VAL_KEY7, "little");
        YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
        final FeedItem current = feedItems.get(position);
        holder.Title.setText(current.getTitle());
        holder.Description.setText(current.getDescription());
        if(theme.equals("big")) {
            holder.Date.setText(current.getPubDate());
            Picasso.with(context).load(current.getThumbnailUrl()).resize(150, 100).into(holder.Thumbnail);
        } else if (theme.equals("little")) {
            Picasso.with(context).load(current.getThumbnailUrl()).resize(150, 100).into(holder.Thumbnail);
        }
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, NewsDetails.class);
                intent.putExtra("Link", current.getLink());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        int size = 0;
        if(feedItems != null){
            size = feedItems.size();
        }
        return size;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView Title, Description, Date;
        ImageView Thumbnail;
        CardView cardView;

        public MyViewHolder(View itemView) {
            super(itemView);
            Title = (TextView) itemView.findViewById(R.id.title_text);
            Description = (TextView) itemView.findViewById(R.id.description_text);
            Date = (TextView) itemView.findViewById(R.id.date_text);
            Thumbnail = (ImageView) itemView.findViewById(R.id.thumb_img);
            cardView = (CardView) itemView.findViewById(R.id.cardview);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在数组列表中添加项目时,在第二个位置添加空数据,在适配器类中,在第二个位置添加视图。