在Recycler视图中显示广告

时间:2018-04-05 07:16:56

标签: android android-recyclerview ads recyclerview-layout

我正在使用Recycler View,其中我必须在GridLayout Manager中显示两个项目,并且跨度计数为2.

GridLayoutManager gridLayoutManager;

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

newsRecycler.setLayoutManager(gridLayoutManager);

主要问题是我必须在四个项目(即2行)后展示广告 这将涵盖移动的宽度。但由于跨度计数,广告也将进入网格布局。任何帮助将不胜感激!!。

输出如下: -

enter image description here

预期产出: -

enter image description here

适配器类: -

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>

1 个答案:

答案 0 :(得分:2)

好的,所以终于找到了一个解决方案。我做错了是错误的条件,而不是让正确的布局膨胀,即ads_layout。

以下是最终的Adapter Class.Rest的所有内容都是相同的。在 onCreateViewHolder 中更改了每4个项目后位置5的广告布局。

 @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);

        }

    }

以及以下功能的一些变化: -

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

希望将来可能对某人有所帮助。谢谢。