Search code examples
androidandroid-recyclerviewadsrecyclerview-layout

Showing Ads in Recycler View


I am working with Recycler View in which i have to show two items in GridLayout Manager and span count of 2.

GridLayoutManager gridLayoutManager;

gridLayoutManager = new GridLayoutManager(getActivity(), 2);

newsRecycler.setLayoutManager(gridLayoutManager);

The main problem is that i have to show Ads after four items (i.e. 2 rows) that will cover width of mobile.But due to Span count the ads are also coming in Grid layout.Any help would be appreciated !!.

Output is coming like this:-

enter image description here

Expected Output:--

enter image description here

Adapter Class :-

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {



private List<HomeNewsDataModel> homeNewsDataModels;

public NewsAdapter(FragmentActivity activity, List<HomeNewsDataModel> homeNewsDataModels) {
    this.activity = activity;
    this.homeNewsDataModels=homeNewsDataModels;

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    View v = null;



        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item_layout, viewGroup, false);
        return new ContentViewHolder(v);
    }


}

private final int  CONTENT_TYPE=1;

private final int AD_TYPE=2;

@Override
public int getItemViewType(int position)
{
    if (position % 4 == 0)
        return AD_TYPE;
    return CONTENT_TYPE;
}


class ViewHolder extends RecyclerView.ViewHolder {

     public ViewHolder(View itemView) {
        super(itemView);


    }


}

class ContentViewHolder extends ViewHolder {



    @BindView(R.id.news_item)
    LinearLayout newsItem;

    @BindView(R.id.news_image)
    ImageView newsImage;

    @BindView(R.id.tv_title)
    TextView tvTitle;


    @BindView(R.id.play_video_bt)
    Button playVideoBt;

    @BindView(R.id.news_main)
    LinearLayout news_main;




    public ContentViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);


    }




}

class ADViewHolder extends ViewHolder {



    @BindView(R.id.ad_item)
    LinearLayout ad_item;

    @BindView(R.id.ad_image_image)
    ImageView ad_image_image;




    public ADViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);


    }

    private void bindData() {






    }



}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {


}


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

}

news_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/news_item"
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/generic_content_padding">

    <LinearLayout
        android:id="@+id/news_main"
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="visible"
        >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/news_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/img_place_holder"/>

        <Button
            android:id="@+id/play_video_bt"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:visibility="gone"
            android:background="@drawable/play_video_icon"
            android:layout_centerInParent="true"
            />

    </RelativeLayout>


    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title 1"
        android:textSize="@dimen/text_medium_small"
        android:maxLines="2"
        android:textColor="@color/app_black"
        android:layout_marginTop="@dimen/generic_content_padding"

        />
    </LinearLayout>



</LinearLayout>

ad_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      android:id="@+id/ad_item"
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">

            <ImageView
             android:id="@+id/ad_image_image"
             android:layout_width="match_parent"
             android:layout_height="100dp"
             android:adjustViewBounds="true"
             android:src="@drawable/img_place_holder"/>


     </LinearLayout>

Solution

  • Ok, so I finally found a solution. What I was doing wrong was putting the wrong condition and not inflating the right layout - ads_layout.

    Here is the final Adapter Class. The rest is the same. Changes made in onCreateViewHolder, inflating ad layout at position 5 after every 4 items.

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    
            View v = null;
    
            if (viewType == CONTENT_TYPE) {    
                v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item_layout, viewGroup, false);
                return new ContentViewHolder(v);
            } else {
                v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.ad_layout, viewGroup, false);
                return new ADViewHolder(v);
            }
        }
    

    and some changes in the following function:

        @Override
        public int getItemViewType(int position) {
            if ((position+1) % 5 == 0 && (position+1) != 1) {
                return AD_TYPE;
            }
            return CONTENT_TYPE;
        }
    

    Hope it might help someone in the future. Thanks.