Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to place ads in between recyclerview

i need to place ads in between recyclerview, i tried one method it works nice and ads loads but problem is ads are overlapped on list view. anyone please help me thank you in advance

Below is the code for Adapter.class

    public class NewsAdapter extends RecyclerView.Adapter<ViewHolder> {

    private static final int CONTENT_TYPE = 0;
    private static final int AD_TYPE = 1;
    private int[] viewTypes;
    int Holderid;
    private List<Newslist> newslists;
    private Context context;


    public NewsAdapter(List<Newslist> newslists, Context context) {
        super();
        //Getting all the news updates
        this.newslists = newslists;
        this.context=context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Inflate the layout, initialize the View Holder
        ViewHolder holder = null;
        if (viewType == CONTENT_TYPE) {
            View newsView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_list, parent, false);
            holder=new ItemViewHolder(newsView);
        } else {
            View adView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_ads, parent, false);
            holder=new AdViewHolder(adView);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        if (position % 6 == 3) {
            AdViewHolder holder = (AdViewHolder) viewHolder;
            //Load the Ad
            AdRequest request = new AdRequest.Builder()
                    .build();
            holder.adView.loadAd(request);

        } else  {

            ItemViewHolder holder = (ItemViewHolder) viewHolder;
            Newslist newslist = newslists.get(holder.getAdapterPosition());

            holder.message.setText(newslist.getMessage());

            //Image loading using singleton class
            ImageLoader imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
            holder.imageView.setImageUrl(newslist.getImageurl(), imageLoader);
            holder.imageView.setDrawingCacheBackgroundColor(R.mipmap.ic_launcher);
            holder.time.setText(newslist.getTime());
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        if (position % 6 ==3)
            return AD_TYPE;
        return CONTENT_TYPE;
    }
    // return newslists.get(position).getViewType();
}

class ViewHolder extends RecyclerView.ViewHolder {
    ViewHolder(View v) {
        super(v);
    }
}

class ItemViewHolder extends ViewHolder {

    TextView message;
    NetworkImageView imageView;
    TextView time;
    RelativeLayout relativeLayout;

    ItemViewHolder(View itemView) {
        super(itemView);
        message = (TextView) itemView.findViewById(R.id.headline);
        time = (TextView) itemView.findViewById(R.id.time);
        imageView = (NetworkImageView) itemView.findViewById(R.id.networkImageView2);
        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
    }
}

class AdViewHolder extends ViewHolder {
    NativeExpressAdView adView;

    AdViewHolder(View v) {
        super(v);
        adView = (NativeExpressAdView) v.findViewById(R.id.nativeAdView);
    }
    }
like image 898
Gokul Gowda Avatar asked Nov 25 '16 07:11

Gokul Gowda


People also ask

How do I add space between items in RecyclerView?

The simplest way is to add top/bottom margins around the first item in the adapter's row. android:layout_marginBottom="4dp". (Note adding the margins to the parent layout won't cut it.)

What is nested RecyclerView?

A nested RecyclerView is an implementation of a RecyclerView within a RecyclerView. An example of such a layout can be seen in a variety of apps such as the Play store where the outer (parent) RecyclerView is of Vertical orientation whereas the inner (child) RecyclerViews are of horizontal orientations.

What does notifyDataSetChanged do in RecyclerView?

notifyDataSetChanged. Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.


1 Answers

    public class NewsAdapter extends RecyclerView.Adapter<ViewHolder> {

        private static final int CONTENT_TYPE = 0;
        private static final int AD_TYPE = 1;
        private int[] viewTypes;
        int Holderid;
        private List<Newslist> newslists;
        private Context context;


        public NewsAdapter(List<Newslist> newslists, Context context) {
            super();
            //Getting all the news updates
            this.newslists = newslists;
            this.context=context;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //Inflate the layout, initialize the View Holder
            ViewHolder holder = null;
            if (viewType == CONTENT_TYPE) {
                View newsView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_list, parent, false);
                holder=new ItemViewHolder(newsView);
            } else if(viewType == AD_TYPE){
                View adView = LayoutInflater.from(parent.getContext()).inflate(R.layout.native_ads, parent, false);
                holder=new AdViewHolder(adView);
            }
            return holder;
        }

        @Override
        public void onBindViewHolder(final NewsAdapter.ItemViewHolder holder, final int position) {

         if(holder.HOLDER == 1){

            }else {
               final int pos=position-Math.round(position / 6);

                 final Newslist objnewslist=newslists.get(pos);

                holder.message.setText(objnewslist.getMessage());

                //Image loading using singleton class
                ImageLoader imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
                holder.imageView.setImageUrl(objnewslist.getImageurl(), imageLoader);
                holder.imageView.setDrawingCacheBackgroundColor(R.mipmap.ic_launcher);
                holder.time.setText(objnewslist.getTime());
            }
        }

        @Override
        public int getItemCount() {
        int size = newslists.size();
    if(newslists.size() > 0) {
        return newslists.size() + Math.round(newslists.size()/6);
    }
            return newslists.size();
        }

        @Override
        public int getItemViewType(int position) {
           if ((position+1)%6==0){
                return AD_TYPE;
            }
            return CONTENT_TYPE;
        }
        // return newslists.get(position).getViewType();
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView message;
        NetworkImageView imageView;
        TextView time;
        RelativeLayout relativeLayout;
        int HOLDER;
        AdView madview;

        ItemViewHolder(View itemView , int viewType) {
            super(itemView);
            if (viewType == AD_TYPE) {
                    madview = (AdView) itemView.findViewById(R.id.adView);
                    AdRequest adreq = new AdRequest.Builder().build();
                    madview.loadAd(adreq);
                    HOLDER = AD_TYPE;
                }else {
            message = (TextView) itemView.findViewById(R.id.headline);
            time = (TextView) itemView.findViewById(R.id.time);
            imageView = (NetworkImageView) itemView.findViewById(R.id.networkImageView2);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
HOLDER = CONTENT_TYPE;
            }
        }
    }

Check the above code this works fine.

like image 67
abhil nair Avatar answered Oct 21 '22 16:10

abhil nair