如何将AdMod原生广告与Recycler视图一起使用?

时间:2017-03-24 01:00:33

标签: java android admob recycle google-admob

我想在我的应用的主要活动中使用Google Admob展示广告。我已经通过谷歌开发者网站获得帮助来显示所有显示广告的代码,但是当我运行我的应用时,广告没有显示。我不知道发生了什么问题:

enter image description here

MainActivity:

package com.ali.nokt_nokt.mdhk.glopal;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import com.ali.nokt_nokt.mdhk.R;
import com.ali.nokt_nokt.mdhk.adaptr.Post;
import com.ali.nokt_nokt.mdhk.adaptr.PostsAdapter;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

public MainActivity extends AppCompatActivity
{
    RecyclerView recyclerview;
    PostsAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.face);
        //Set back button on action bar
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);




        recyclerview = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.tagarb.esy.es/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        retrofit.create(API.class).getbast_B().enqueue(new Callback<List<Post>>() {
            @Override
            public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                final List<Post> postList = response.body();
                adapter = new PostsAdapter(postList, Bast_2.this);
                recyclerview.setAdapter(adapter);

                final Post post = new Post();
                post.setTitle("لحن الحياه");
                post.setBody("وقـالت لـي الأرضُ - لمـا سـألت:\tأيــا أمُّ هــل تكــرهين البشــرْ?\n" +
                        "أُبــارك فـي النـاس أهـلَ الطمـوح\tومــن يســتلذُّ ركــوبَ الخــطرْ\n" +
                        "وألْعــنُ مــن لا يماشــي الزمـانَ\tويقنـــع بــالعيْشِ عيشِ الحجَــرْ\n" +
                        "هــو الكــونُ حـيٌّ, يحـبُّ الحيـاة\tويحــتقر المَيْــتَ, مهمــا كــبُرْ\n" +
                        "فـلا الأفْـق يحـضن ميْـتَ الطيـورِ\tولا النحــلُ يلثــم ميْــتَ الزهـرْ\n" +
                        "ولــولا أمُومــةُ قلبِــي الــرّؤوم\tلَمَــا ضمّــتِ الميْـتَ تلـك الحُـفَرْ\n" +
                        "فــويلٌ لمــن لــم تشُــقه الحيـا\tة, مِــن لعنــة العــدم المنتصِـرْ!");

                new CountDownTimer(4000, 1000) {

                    @Override
                    public void onTick(long millisUntilFinished) {

                    }

                    @Override
                    public void onFinish() {
                        postList.set(0, post);
                        adapter.notifyItemChanged(0);
                    }
                }.start();
            }

            @Override
            public void onFailure(Call<List<Post>> call, Throwable t) {

            }
        });
    }


    public interface API {
        @GET("bast_B.php")
        Call<List<Post>> getbast_B();
    }



    //Options Menu Create
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    //Options Menu on Item Click
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

//Take back to Home Screen on pressing back button
        if (id == android.R.id.home) {

//Simply finish this activity
            Bast_2.this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);

    }
}

这是MyAdapter.java:

package com.ali.nokt_nokt.mdhk.adaptr;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ali.nokt_nokt.mdhk.R;



import java.util.List;

/**
 * Created by developerhendy on 9/8/16.
 * © 2016.
 */
public class PostsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {



    private List<Post> posts;
    private Context context;

    public PostsAdapter(List<Post> posts, Context ctx) {
        this.posts = posts;
        this.context = ctx;
    }




    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 456) {
            return new AdsHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.ads_row, parent, false));

        } else {
            return new PostHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.post_row, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == 456) {
            AdsHolder adsHolder = (AdsHolder) holder;



            // if we wanna do some thing with ads row for example change img we do it here



        } else {
            PostHolder postHolder = (PostHolder) holder;

            if (viewType == 123) {
                postHolder.postLayout.setBackgroundColor(Color.parseColor("#23c486"));
                postHolder.postTitle.setTextColor(Color.parseColor("#ffffff"));
                postHolder.postDesc.setTextColor(Color.parseColor("#ffffff"));
            }

            final Post post = posts.get(position);
            postHolder.postTitle.setText(post.getTitle());
            postHolder.postDesc.setText(post.getBody());



        }

    }





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

    @Override
    public int getItemViewType(int position) {
        if (position == 2) {
            return 123;
        } else if (position == 3) {
            return 456;
        } else {
            return super.getItemViewType(position);
        }
    }



    public class AdsHolder extends RecyclerView.ViewHolder {

        public AdsHolder(View itemView) {
            super(itemView);
        }
    }

    public class PostHolder extends RecyclerView.ViewHolder {
        private TextView postTitle;
        private TextView postDesc;
        private LinearLayout postLayout;


        public PostHolder(final View itemView) {
            super(itemView);
            postTitle = (TextView) itemView.findViewById(R.id.post_title);
            postDesc = (TextView) itemView.findViewById(R.id.post_desc);
            postLayout = (LinearLayout) itemView.findViewById(R.id.row_container);


            postTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Post post = posts.get(getAdapterPosition());
                    Toast.makeText(context, "You clicked on " + post.getTitle(), Toast.LENGTH_SHORT).show();
                }
            });

            postLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Post post = posts.get(getAdapterPosition());

                    Intent i = new Intent(context, Pag.class);
                    i.putExtra("Titler", post.getTitle());
                    i.putExtra("Desc", post.getBody());
                    context.startActivity(i);
                }
            });



        }
    }
    }

这个activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ececec"
    tools:context="com.ali.nokt_nokt.mdhk.glopal.Bast_2">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

和这个admob.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"

    android:orientation="vertical" android:layout_width="360dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="8dp"
    android:layout_height="132dp">


    <com.google.android.gms.ads.AdView

        android:id="@+id/adView"
        android:layout_width="360dp"
        android:layout_height="132dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>


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

0 个答案:

没有答案