有没有办法在Android Recycler View中显示Google Admob

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

MainActivity

 import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.support.v7.app.AlertDialog; 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 android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MainActivity extends AppCompatActivity { public RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; private View view; private Button button; String[] mDataSet = new String[]{ "1","2" }; String[] data = new String[]{"Sample Activty " }; private Context context; private Intent sharingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AdView mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); mRecyclerView.addOnItemTouchListener( new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { // do whatever Toast.makeText(view.getContext(), "position = " + position, Toast.LENGTH_SHORT).show(); } context.startActivity(intent); } }) ); // use a linear layout manager LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getResources())); // specify an adapter (see also next example) mAdapter = new MyAdapter(mDataSet); mRecyclerView.setAdapter(mAdapter); 

这是MyAdapter.java

 import android.content.Context; import android.content.Intent; import android.nfc.Tag; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.TextView; import android.widget.Toast; public class MyAdapter extends RecyclerView.Adapter { // private String[] mDataset; public String[] mDataset; private String mItem; private Context context; AdapterView.OnItemClickListener itemClickListener; // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder public class ViewHolder extends RecyclerView.ViewHolder { // each data item is just a string in this case public TextView mTextView; public TextView mFootView; public ViewHolder(View v) { super(v); // context = v.getContext(); mTextView = (TextView) v.findViewById(R.id.firstLine); // mFootView= (TextView) v.findViewById(R.id.secondLine); } } // Provide a suitable constructor (depends on the kind of dataset) public MyAdapter(String[] myDataset) { mDataset = myDataset; } // Create new views (invoked by the layout manager) @Override public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) { // create a new view View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_main, parent, false); // set the view's size, margins, paddings and layout parameters ViewHolder vh = new ViewHolder(v); return vh; } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder holder, int position) { // - get element from your dataset at this position // - replace the contents of the view with that element holder.mTextView.setText(mDataset[position]); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return mDataset.length; // return mDataset.size(); } } 

这个activity_main.xml

        

这是完整的例子。 我刚刚为您创建了一个示例应用程序。

主要活动

 import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import java.util.ArrayList; import java.util.List; public class Main extends AppCompatActivity { public static final String TAG = Main.class.getSimpleName(); private Context mContext; private List mList; private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mContext = this; toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mList = new ArrayList(); for(int i=0;i<10;i++){ MyListModel myString = new MyListModel(); myString.setName(i+" - I love Paris"); myString.setViewType(1); mList.add(myString); } //Place two Admob Ads at position index 1 and 5 in recyclerview MyListModel myString1 = new MyListModel(); myString1.setViewType(2); mList.add(1,myString1); MyListModel myString2 = new MyListModel(); myString2.setViewType(2); mList.add(5,myString2); // use a linear layout manager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); // specify an adapter (see also next example) mAdapter = new ListAdopter(this, mList); mRecyclerView.setAdapter(mAdapter); } } 

RecyclerViewAdopter

 import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import java.util.List; public class RecyclerViewAdopter extends RecyclerView.Adapter{ private Context mContext; private List mList; public RecyclerViewAdopter(Context mContext, List mList) { this.mList = mList; this.mContext = mContext; } public static class MyViewHolder extends RecyclerView.ViewHolder { public TextView name; public MyViewHolder(View view) { super(view); name = (TextView) view.findViewById(R.id.listView_name); } } public static class ViewHolderAdMob extends RecyclerView.ViewHolder { public AdView mAdView; public ViewHolderAdMob(View view) { super(view); mAdView = (AdView) view.findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); mAdView.loadAd(adRequest); } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { RecyclerView.ViewHolder viewHolder = null; LayoutInflater inflater = LayoutInflater.from(parent.getContext()); switch(viewType){ case 1:{ View v = inflater.inflate(R.layout.list_item_1, parent, false); viewHolder = new MyViewHolder(v); break; } case 2:{ View v = inflater.inflate(R.layout.list_item_admob, parent, false); viewHolder = new ViewHolderAdMob(v); break; } } return viewHolder; } @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { MyListModel model = mList.get(holder.getAdapterPosition()); switch(holder.getItemViewType()){ case 1:{ MyViewHolder viewHolder = (MyViewHolder) holder; viewHolder.name.setText(model.getName()); break; } case 2:{ break; } } } @Override public int getItemViewType(int position) { return mList.get(position).getViewType(); } @Override public int getItemCount() { return mList.size(); } } 

main.xml中

      

list_item_1.xml

     

list_item_admob.xml

   

在此处输入图像描述

更新9-12-2016

MyListModel

 public class MyListModel { private String name; private int viewType; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getViewType() { return viewType; } public void setViewType(int viewType) { this.viewType = viewType; } }