Join FlipAndroid.COM Telegram Group: https://t.me/joinchat/F_aqThGkhwcLzmI49vKAiw


¿Hay alguna manera de mostrar Google Admob en Android Recycler View

Quiero mostrar anuncios con google Admob en la actividad principal de mi aplicación. He colocado todo el código necesario para mostrar anuncios al obtener ayuda del sitio de google-developers pero cuando ejecuto mi aplicación, los anuncios no se muestran. No sé cuál es el problema que ocurre:

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

Y este es 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<MyAdapter.ViewHolder> { // 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(); } } 

Y esta activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:ads="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical" tools:context=".MainActivity" android:background="@drawable/main_redrose" > <!-- A RecyclerView with some commonly used attributes --> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" android:divider="@drawable/list_divider"/> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" ads:adSize="BANNER" ads:adUnitId="@string/banner_ad_unit_id"> </com.google.android.gms.ads.AdView> </LinearLayout> 

One Solution collect form web for “¿Hay alguna manera de mostrar Google Admob en Android Recycler View”

Aquí está el ejemplo completo. Acabo de crear una aplicación de ejemplo para usted.

Actividad principal

 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<MyListModel> 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<MyListModel>(); 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<RecyclerView.ViewHolder>{ private Context mContext; private List<MyListModel> mList; public RecyclerViewAdopter(Context mContext, List<MyListModel> 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

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.kyanogen.ui.ScyllaListFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:scrollbarThumbVertical="@color/primary_dark" android:scrollbarStyle="insideInset" android:scrollbarSize="4dp" android:layout_below="@+id/toolbar"/> </RelativeLayout> 

List_item_1.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/listView_name" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="30dp" android:textSize="16sp"/> </LinearLayout> 

List_item_admob.xml

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

Introduzca aquí la descripción de la imagen

Actualización 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; } } 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.