2

I'm using FastAdapter in my app and I want to place ads randomly in between of RecyclerView. For ex - like an ad after 3 RecyclerView items then after 4, then after 2 and so on and so forth.

This is how I'm using FastAdapter:

FastItemAdapter<HRequest> fastItemAdapter = new FastItemAdapter<>();
fastItemAdapter.withSelectable(true);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());

HRequest hRequest = new HRequest(imageUID);
fastItemAdapter.add(helpRequest);
recyclerView.setAdapter(fastItemAdapter);

Here's HRequest.java file's code:

public class HRequest extends AbstractItem<HRequest, HRequest.ViewHolder> {

        public String imageURL;

        public HRequest() {

        }

        public HRequest(String imageURL) {
            this.imageURL = imageURL;
        }

        // Fast Adapter methods
        @Override
        public int getType() {
            return R.id.recycler_view;
        }
        @Override
        public int getLayoutRes() {
            return R.layout.h_request_list_row;
        }
        @Override
        public void bindView(ViewHolder holder) {
            super.bindView(holder);

            holder.imageURL.setText(imageURL);

        }
        // Manually create the ViewHolder class
        protected static class ViewHolder extends RecyclerView.ViewHolder {

            TextView imageURL;

            public ViewHolder(View itemView) {
                super(itemView);
                imageURL = (TextView)itemView.findViewById(R.id.imageURL);

if (!imageURL.getText().toString().isEmpty()) {

if (imageURL.getText().toString().startsWith("https://firebasestorage.googleapis.com/") || imageURL.getText().toString().startsWith("content://")) {
                    Picasso.with(itemView.getContext())
                            .load(imageURL.getText().toString())
                            .into(homelessImage);
                } else {
                    Toast.makeText(itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
                }

            } else {
                Toast.makeText(itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
            }

            }
        }

    }

How can I achieve what I want?

halfer
  • 19,824
  • 17
  • 99
  • 186
Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • Good question, but there's no need to add `please see details` to your titles. Keep them as succinct and chat-free as possible! Thanks. – halfer Jun 25 '16 at 16:53

1 Answers1

2

Your code was not enough for me to fully get the picture but this is what I came up with. Let me know if this works or there's something I am missing.

public class HRequest extends AbstractItem<HRequest, HRequest.ViewHolder> {
    public static int count = 0;
    public static int random = 0;
    public String imageURL;

    public HRequest() {

    }

    public HRequest(String imageURL) {
        this.imageURL = imageURL;
        Random rand = new Random();
        random = random.nextInt(4); 
    }

    // Fast Adapter methods
    @Override
    public int getType() {
        return R.id.recycler_view;
    }
    @Override
    public int getLayoutRes() {
        return R.layout.h_request_list_row;
    }
    @Override
    public void bindView(ViewHolder holder) {
        super.bindView(holder);

        holder.imageURL.setText(imageURL);


if(count >= random ){

   // Load Your Ad
     random = random.nextInt(4);  // Reset the counter to random integer
       count = 0;
     }else{
            count++;
 if (!imageURL.getText().toString().isEmpty()) {

   if(imageURL.getText().toString().startsWith("https://firebasestorage.googleapis.com/") || imageURL.getText().toString().startsWith("content://")) {
                    Picasso.with(itemView.getContext())
                            .load(imageURL.getText().toString())
                            .into(homelessImage);
            } else {
                Toast.makeText(itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
            }

        } else {
            Toast.makeText(itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
        }

        }

    }
    // Manually create the ViewHolder class
    protected static class ViewHolder extends RecyclerView.ViewHolder {

        TextView imageURL;

        public ViewHolder(View itemView) {
            super(itemView);
            imageURL = (TextView)itemView.findViewById(R.id.imageURL);

    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
M.Waqas Pervez
  • 2,492
  • 2
  • 19
  • 33
  • should I place ads in `R.layout.h_request_list_row;` or some other layout file? – Hammad Nasir Jun 25 '16 at 09:49
  • can you take a look at this too: http://stackoverflow.com/questions/41125004/notification-not-getting-removed-on-clicking-action-button-even-after-providing Please? – Hammad Nasir Dec 14 '16 at 08:59
  • hey... I have a related question: how to show the ads only on the top of the recyclerview? – Hammad Nasir Dec 14 '16 at 12:28
  • hello there.. need your help again with a similar question. please take a look: https://stackoverflow.com/questions/46638945/how-to-show-cardview-containing-ads-randomly-but-not-before-3-cards-and-not-afte – Hammad Nasir Oct 18 '17 at 11:53