@Chesterfield25

Как встроить меж страничную рекламу admob?

Как встроить меж страничную рекламу admob при каждом 5-ом нажатие?

Есть MainActivity.class

public class MainActivity extends AppCompatActivity {
    ArrayList<Section> sections = new ArrayList<>();

    private AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        readMainJson();
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager(), sections);
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);


        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);


    }

    void readMainJson() {
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.main)));
            String temp = br.readLine();
            while (temp != null) {
                sb.append(temp);
                temp = br.readLine();
            }
            JSONArray mainJson = new JSONArray(sb.toString());

            for (int i = 0; i < mainJson.length(); i++) {
                JSONObject sectionJson = mainJson.getJSONObject(i);
                Section section = new Section();
                section.setId(sectionJson.getString("id"));
                section.setTitle(sectionJson.getString("title"));

                ArrayList<Page> pagesList = new ArrayList<>();
                JSONArray pagesJSonArray = sectionJson.getJSONArray("pages");
                for (int j=0; j < pagesJSonArray.length();j++ ) {
                    JSONObject pageJson = pagesJSonArray.getJSONObject(j);
                    Page page = new Page();
                    page.setId(pageJson.getInt("page_id"));
                    page.setPhoto(pageJson.getString("photo"));

                    ArrayList<String> pageImages = new ArrayList<>();
                    JSONArray pageImagesJSonArray = pageJson.getJSONArray("imgs");
                    for (int k=0; k < pageImagesJSonArray.length(); k++) {
                        String imgName = pageImagesJSonArray.getString(k);
                        pageImages.add(imgName);
                    }
                    page.setImgs(pageImages);
                    pagesList.add(page);
                }
                section.setPages(pagesList);
                sections.add(section);
            }

        } catch (IOException e) {
            Toast toast = Toast.makeText(this, "Error: opening json", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } catch (JSONException e) {
            Toast toast = Toast.makeText(this, "Error: problems in json", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                Toast toast = Toast.makeText(this, "Error: closing json", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    }

}


Нужно показать рекламу при переходе на ViewFullImageActivity.class

PagesRecyclerViewAdapter.class

public class PagesRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    ArrayList<Page> pages;



    public PagesRecyclerViewAdapter(Context context, ArrayList<Page> pages) {
        this.context = context;
        this.pages = pages;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.page, parent, false);
        CustomViewHolder customViewHolder = new CustomViewHolder(view);
        return customViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CustomViewHolder customViewHolder = (CustomViewHolder)holder;
        Page page = pages.get(position);
        String pagePhoto = page.getPhoto();
        int imgId = context.getResources().getIdentifier(pagePhoto, "drawable", context.getPackageName());
        customViewHolder.pagePhotoImageView.setImageResource(imgId);
        customViewHolder.pageId = position;
    }

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

    class CustomViewHolder extends RecyclerView.ViewHolder {
        int pageId;
        ImageView pagePhotoImageView;

        public CustomViewHolder(@NonNull View itemView) {
            super(itemView);
            pagePhotoImageView = itemView.findViewById(R.id.image_view_page_photo);
            pagePhotoImageView.setOnClickListener(v -> {
                    Page page = pages.get(pageId);
                    ArrayList<String> pageImages = page.getImgs();
                    Bundle bundle = new Bundle();
                    bundle.putStringArrayList("page_images", pageImages);
                    Intent intentViewFullImageActivity = new Intent(context, ViewFullImageActivity.class);
                    intentViewFullImageActivity.putExtras(bundle);
                    intentViewFullImageActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intentViewFullImageActivity);
            });
        }
    }
}
  • Вопрос задан
  • 44 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы