我正在跟踪由Google提供的link内的RecyclerView内的NativeExpressAdView教程。在本教程中,List被视为对象模型类。但我的Recylerview有我自己的自定义Model类名称:Artical。这就是我在代码中进行一些更改的原因。我的代码如下。
ArticleAdapter.java
package com.prof.rssparser.example;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.ads.NativeExpressAdView;
import com.prof.rssparser.Article;
import com.squareup.picasso.Picasso;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import static com.prof.rssparser.example.R.drawable.placeholder;
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {
private List <Article> articles;
private int rowLayout;
private Context mContext;
WebView articleView;
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1;
public ArticleAdapter(List<Article> list, int rowLayout, Context context)
{
this.articles = list;
this.rowLayout = rowLayout;
this.mContext = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView pubDate;
ImageView image;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
pubDate = (TextView) itemView.findViewById(R.id.pubDate);
image = (ImageView)itemView.findViewById(R.id.image);
}
}
public class NativeExpressAdViewHolder extends ArticleAdapter.ViewHolder {
NativeExpressAdViewHolder(View view) {
super(view);
}
}
@Override
public int getItemCount() {
return articles == null ? 0 : articles.size();
}
@Override
public int getItemViewType(int position) {
return (position % 8 == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE
: MENU_ITEM_VIEW_TYPE;
}
@Override
public long getItemId(int item) {
// TODO Auto-generated method stub
return item;
}
public void clearData(){
if (articles != null)
articles.clear();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
switch (i)
{
case MENU_ITEM_VIEW_TYPE:
View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new ViewHolder(v);
case NATIVE_EXPRESS_AD_VIEW_TYPE:
// fall through
default:
View nativeExpressLayoutView = LayoutInflater.from(
viewGroup.getContext()).inflate(R.layout.row,
viewGroup, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
}
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case MENU_ITEM_VIEW_TYPE:
Article currentArticle = articles.get(position);
Locale.setDefault(Locale.getDefault());
Date date = currentArticle.getPubDate();
SimpleDateFormat sdf = new SimpleDateFormat();
sdf = new SimpleDateFormat("dd MMMM yyyy");
final String pubDateString = sdf.format(date);
viewHolder.title.setText(currentArticle.getTitle());
Picasso.with(mContext)
.load(currentArticle.getImage())
.placeholder(placeholder)
.fit()
.centerCrop()
.into(viewHolder.image);
viewHolder.pubDate.setText(pubDateString);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//show article content inside a dialog
articleView = new WebView(mContext);
articleView.getSettings().setLoadWithOverviewMode(true);
String title = articles.get(position).getTitle();
String content = articles.get(position).getContent();
articleView.getSettings().setJavaScriptEnabled(true);
articleView.setHorizontalScrollBarEnabled(false);
articleView.setWebChromeClient(new WebChromeClient());
articleView.loadDataWithBaseURL(null, "<style>img{display: inline; height: auto; max-width: 100%;} " +
"</style>\n" + "<style>iframe{ height: auto; width: auto;}" + "</style>\n" + content, null, "utf-8", null);
android.support.v7.app.AlertDialog alertDialog = new android.support.v7.app.AlertDialog.Builder(mContext).create();
alertDialog.setTitle(title);
alertDialog.setView(articleView);
alertDialog.setButton(android.support.v7.app.AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
((TextView) alertDialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
}
});
break;
case NATIVE_EXPRESS_AD_VIEW_TYPE:
// fall through
default:
NativeExpressAdViewHolder nativeExpressHolder = (NativeExpressAdViewHolder) viewHolder;
//i know below casting is wrong but what is right method for this?
NativeExpressAdView adView = (NativeExpressAdView)(Object)articles.get(position);
ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView;
if (adCardView.getChildCount() > 0) {
adCardView.removeAllViews();
}
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeView(adView);
}
// Add the Native Express ad to the native express ad view.
adCardView.addView(adView);
}
}
}
Artical.java
package com.prof.rssparser;
import java.util.Date;
public class Article {
private String title;
private String author;
private String link;
private Date pubDate;
private String description;
private String content;
private String image;
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getLink() {
return link;
}
public Date getPubDate() {
return pubDate;
}
public String getDescription() {
return description;
}
public String getContent() {
return content;
}
public String getImage() {
return image;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setLink(String link) {
this.link = link;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public void setDescription(String description) {
this.description = description;
}
public void setContent(String content) {
this.content = content;
}
public void setImage(String image) {
this.image = image;
}
@Override
public String toString() {
return "Article{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", link='" + link + '\'' +
", pubDate=" + pubDate +
", description='" + description + '\'' +
", content='" + content + '\'' +
", image='" + image + '\'' +
'}';
}
}
我收到此错误:
java.lang.ClassCastException: com.prof.rssparser.Article cannot be cast to com.google.android.gms.ads.NativeExpressAdView
at com.prof.rssparser.example.ArticleAdapter.onBindViewHolder(ArticleAdapter.java:201)
at com.prof.rssparser.example.ArticleAdapter.onBindViewHolder(ArticleAdapter.java:48)
正如您所见,我Object
将Artical
投放到(Object)articles.get(position)
。使用Casting是错误的还是有其他方法可以解决这个问题?如何在自定义模型列表中添加NativeExpressAdView。请帮助我,抱歉格式不好,我是菜鸟。