2016-06-24 8 views
1

私のアプリでFastAdapterを使用しています。広告を無作為にRecyclerViewの間に配置したいと考えています。たとえば、3の後の広告の場合は、RecyclerView、その後は4、その後は2などとなります。RecyclerViewの間に広告をランダムに配置する方法は?

は、これは私が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); 

ここHRequest.javaファイルのコードです:

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

      } 
     } 

    } 

どのように私は私が望むものを達成することができますか?

+0

良い質問ですが、あなたのタイトルに「please see details」を追加する必要はありません。できるだけ簡潔でチャットフリーにしてください!ありがとう。 – halfer

答えて

2

あなたのコードでは私が完全に画像を取得するには十分ではありませんでしたが、これは私が思いついたものです。これが機能するか、私が紛失しているものがあるかどうかを教えてください。

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

    } 

} 
+0

'R.layout.h_request_list_row;'やその他のレイアウトファイルに広告を配置する必要がありますか? –

+0

あなたもこれを見てください:http://stackoverflow.com/questions/41125004/notification-not-getting-removed-on-clicking-action-button-even-after-providingお願いしますか? –

+0

ねえ...私は関連する質問があります:どのようにrecyclerviewの上に広告を表示するだけですか? –

関連する問題