0
我已經實現了一個ViewHolderAdapter
來加載項目並將其顯示在ListView
上,但我還想在某些行中顯示AdMob Banner
。我爲兩個不同的Layouts
創建了自定義Adapter
。但問題是隻顯示兩行,每個Layout
一個,當項目列表超過400個項目時。 有人可以幫助我嗎?在ViewHolderAdapter上加載項目
我下面展示的代碼:
[![enter image description here][1]][1]public class SubliguesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static List<Subligues> items;
public static class ClasGeneralViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
// Campos respectivos de un item
public TextView subligue_name;
public TextView created_at;
public TextView members;
public ImageView icon;
public ClasGeneralViewHolder(View v) {
super(v);
subligue_name = (TextView) v.findViewById(R.id.txt_subligue_name);
created_at = (TextView) v.findViewById(R.id.txt_created_at);
members = (TextView) v.findViewById(R.id.txt_members);
icon = (ImageView) v.findViewById(R.id.imageViewIcon);
v.setOnClickListener(this);
}
public void onClick(View view){
String subligue_name= items.get(getPosition()).getSubligueName();
// Launch RegisterUser
Intent intent = new Intent(view.getContext(),
RegisterUser.class);
// Pasar el nombre de la liga del usuario a RegisterUser.java
intent.putExtra("subligue_name",subligue_name);
view.getContext().startActivity(intent);
((Activity) view.getContext()).finish();
}
}
public static class BannerViewHolder extends RecyclerView.ViewHolder{
// Admob banner
public AdView mAdView;
public BannerViewHolder(View v) {
super(v);
mAdView = (AdView) v.findViewById(R.id.adView);
}
}
@Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return position %10;
}
public SubliguesAdapter(List<Subligues> items) {
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
switch (i) {
case 0:
View v2 = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_banner, viewGroup, false);
return new BannerViewHolder(v2); // view holder for banner items;
case 1:
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.activity_register_existente_abierta_card, viewGroup, false);
return new ClasGeneralViewHolder(v); // view holder for normal items;
}
return null;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
switch (viewHolder.getItemViewType()) {
case 0:
BannerViewHolder viewHolder2 = (BannerViewHolder)viewHolder;
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
viewHolder2.mAdView.loadAd(adRequest);
break;
case 1:
ClasGeneralViewHolder viewHolder0 = (ClasGeneralViewHolder)viewHolder;
viewHolder0.subligue_name.setText(String.valueOf(items.get(i).getSubligueName()));
String created_at = items.get(i).getCreatedAt().substring(0, items.get(i).getCreatedAt().length() - 9);
viewHolder0.created_at.setText(created_at);
viewHolder0.members.setText(String.valueOf(items.get(i).getMembers())+" ");
new ImageDownloaderTask(viewHolder0.icon).execute(items.get(i).getIcon());
break;
}
}
class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public ImageDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
return downloadBitmap(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.no_team);
imageView.setImageDrawable(placeholder);
}
}
}
}
}
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try {
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
int statusCode = urlConnection.getResponseCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (Exception e) {
urlConnection.disconnect();
Log.w("ImageDownloader", "Error downloading image from " + url);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
}
嗨穆罕默德,謝謝你的回答!當列表超過400個時,我仍然只有兩行。看起來像顯示adview時,不再有任何其他的東西。 – agustincola
你有沒有轉換職位?廣告一般爲0,其他1爲1。嘗試一下,然後看看它會顯示多少。 –
嗨,穆罕默德,現在有了這個改變,我剛剛拿到了一行橫幅:(我用改變代碼編輯了我的問題 – agustincola