How to place ads randomly in between of RecyclerView?

后端 未结 1 576
难免孤独
难免孤独 2021-01-15 13:03

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 a

1条回答
  •  鱼传尺愫
    2021-01-15 13:35

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

    0 讨论(0)
提交回复
热议问题