RecyclerView Android内部的AdView

时间:2018-10-09 08:58:24

标签: android android-recyclerview ads

当我在AdView列表项中使用RecyclerView时,广告正在展示,但在RecyclerView滚动中出现滞后。

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/CardViewStyle"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/ll_main">
            <ImageView
                    android:id="@+id/imgmovie"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/banner" />


        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/ll_add"
            android:padding="5dp">

            <com.google.android.gms.ads.AdView
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:id="@+id/adView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                ads:adSize="LARGE_BANNER"
                ads:adUnitId="ca-app-pub-29456479604197"/>
        </LinearLayout>
    </LinearLayout>

</android.support.v7.widget.CardView>

适配器代码

public class MoviesListMainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context con;
    AtmHolder pvh1;
    List<MovieListData> movie_list;

    public MoviesListMainAdapter(boolean loadMore, List<MovieListData> movie_list, Context con) {
        this.movie_list = movie_list;
        this.con = con;


    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        RecyclerView.ViewHolder viewHolder = null;
        if (position == 0) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.movie_list_item_style, viewGroup, false);
            viewHolder = new AtmHolder(v);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof AtmHolder) {
                pvh1 = (AtmHolder) holder;
                AdRequest adRequest = new AdRequest
                        .Builder()
                        .build();
                pvh1.adView.loadAd(adRequest);

                Glide.with(con)
                        .load("Image URL")
                        .placeholder(R.drawable.banner)
                        .crossFade()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(pvh1.imgmovie);
        }
    }

    public int getItemViewType(int position) {
        int a = 0;
        return a;
    }

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

    public static class AtmHolder extends RecyclerView.ViewHolder {
        ImageView imgmovie;
        AdView adView;
        AtmHolder(View itemView) {
            super(itemView);  
            imgmovie = (ImageView) itemView.findViewById(R.id.imgmovie);
            this.adView=itemView.findViewById(R.id.adView);
        }
    }
}

0 个答案:

没有答案