Android SDK Advanced Usage
RecyclerViewAdPlacer
RecyclerViewAdPlacer
is used for displaying dynamic banner ads in RecyclerView
s. Dynamic banner ads have their own predefined positions thus, they will not be loaded until the user scrolls to that specific position.
This comes a few advantages:
- Loading banner ads not at the initial screen load, but rather on user scroll action increases load time performance of your screens.
- Loading banner ads only when they will be visible to user, increases viewability metrics.
- Predefined positions can be updated without the need of sending a new app update, which gives the app owners ability to change the banner positions even when the app is live.
- As the banners are displayed dynamically, app owners can remove the banners from certain pages without the need of a new app update. Just like changing banner positions, this can also be done while the app is live.
Loading and displaying of banner ads are handled within RecyclerViewAdPlacer
which makes integration of banner ads quite easy.
Integration
Step 1: After creating your RecyclerView
and setting its adapter and layout manager properties create a RecyclerViewAdPlacer
object.
Step 2: Once the RecyclerViewAdPlacer
object is created, set its adCategories
and pass your RecyclerView
to the setRecyclerView
function.
Step 3: Finally call your adPlacer
's notifyDataSetChanged()
method.
Done: That's it! Now your app is ready to show dynamic banner ads.
Full Example
- Java
- Kotlin
public class MyActivity extends AppCompatActivity() {private RecyclerView mRecyclerView;private RecyclerViewAdPlacer adPlacer;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Set layout manager and adapter of your recyclerview// ...adPlacer = new RecyclerViewAdPlacer(getApplicationContext());adPlacer.setRecyclerView(mRecyclerView);adPlacer.notifyDataSetChanged();}@Override protected void onResume() {super.onResume();adPlacer.recyclerViewIsVisible(); // Enable auto refresh on loaded banner ads}@Override protected void onPause() {super.onPause();adPlacer.recyclerViewIsInvisible(); // Disable auto refresh on loaded banner ads}private void loadPage() {// Populate your data to be displayed// ...adPlacer.notifyDataSetChanged()}}