我在一个RecyclerView中使用RSS阅读器获取新闻动态,在RecyclerView的索引2处,我想放置一个广告视图,但不知道如何放置。请立即为我解决这个问题。
RecyclerView的代码:
private Context mContext;
private View main;
private RecyclerView m_newsList;
private RecyclerView.LayoutManager m_layoutManager;
private CNewsAdapter m_newsAdapter;
String tagname;
ArrayList<CNewsModel> feeds = new ArrayList<CNewsModel>();
private RelativeLayout m_mainLayout;
private String URL = "http://gadgets.ndtv.com/rss/feeds";
private LinearLayout m_noInternetWarning;
private ImageButton m_BtnRetry;
private ProgressBar m_loadingProgress;
CNewsModel feed;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
main = inflater.inflate(R.layout.reward_news_activity, container, false);
mContext = getActivity();
m_mainLayout = (RelativeLayout) main.findViewById(R.id.news_main_layout);
findViewById();
readRss();
return main;
}
/*Loading news through Rss reader*/
private void readRss() {
if (RewardUtil.isConnected(mContext)) {
new LoadFeed().execute(URL);
} else {
m_noInternetWarning.setVisibility(View.VISIBLE);
m_loadingProgress.setVisibility(View.GONE);
}
}
private void findViewById() {
m_BtnRetry = (ImageButton) main.findViewById(R.id.news_btn_retry);
m_BtnRetry.setOnClickListener(this);
m_noInternetWarning = (LinearLayout) main.findViewById(R.id.news_no_internet_warning);
m_loadingProgress = (ProgressBar) main.findViewById(R.id.news_loadingProgress);
m_newsList = (RecyclerView) main.findViewById(R.id.news_list);
m_layoutManager = new LinearLayoutManager(mContext);
m_newsList.setLayoutManager(m_layoutManager);
}
@Override
public void onClick(View v) {
if (v == m_BtnRetry) {
if (RewardUtil.isConnected(mContext)) {
m_noInternetWarning.setVisibility(View.GONE);
m_loadingProgress.setVisibility(View.VISIBLE);
new LoadFeed().execute(URL);
} else {
CSnackBar.showSnackBarError(m_mainLayout, getResources().getString(R.string.no_internet_connection_warning));
}
}
}
/*Loading news Throudgh Rss reader*/
private class LoadFeed extends AsyncTask<String, Void, ArrayList<CNewsModel>> {
@Override
protected ArrayList<CNewsModel> doInBackground(String... arg0) {
XmlPullParserFactory factory = null;
XmlPullParser parser = null;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
parser = factory.newPullParser();
DefaultHttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet(new URI(arg0[0]));
HttpResponse res = client.execute(method);
InputStream is = res.getEntity().getContent();
parser.setInput(new InputStreamReader(is));
int eventType = parser.getEventType();
feed = new CNewsModel();
String text = "";
while (eventType != XmlPullParser.END_DOCUMENT) {
tagname = parser.getName();
//Log.d(tagname, "tagname");
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("item")) {
feed = new CNewsModel();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("item")) {
feeds.add(feed);
} else if (tagname.equalsIgnoreCase("title")) {
feed.setTitle(text);
} else if (tagname.equalsIgnoreCase("description")) {
feed.setDesc(text);
} else if (tagname.equalsIgnoreCase("link")) {
feed.setLinks(text);
} else if (tagname.equalsIgnoreCase("storyimage")) {
feed.setImage_list(text);
} else if (tagname.equalsIgnoreCase("pubDate")) {
feed.setPub_date(text);
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException | IOException | URISyntaxException e) {
e.printStackTrace();
}
return feeds;
}
@Override
protected void onPostExecute(final ArrayList<CNewsModel> cNewsModels) {
super.onPostExecute(cNewsModels);
m_loadingProgress.setVisibility(View.GONE);
m_newsAdapter = new CNewsAdapter(mContext, cNewsModels, new RecyclerViewClickListener() {
@Override
public void recyclerViewListClicked(View v, int position) {
Toast.makeText(mContext, "Clicked::" + position, Toast.LENGTH_LONG).show();
Intent i = new Intent(mContext, CNewsView.class);
i.putExtra("Link", cNewsModels.get(position).getLinks());
mContext.startActivity(i);
}
});
m_newsList.setAdapter(m_newsAdapter);
}
}
以及适配器的代码
private static final String TAG = CNewsAdapter.class.getSimpleName();
private Context mContext;
private ArrayList<CNewsModel> models;
private static RecyclerViewClickListener itemListener;
public CNewsAdapter(Context context, ArrayList<CNewsModel> mList, RecyclerViewClickListener itemListener) {
this.mContext = context;
this.models = mList;
this.itemListener = itemListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 2) {
View adView = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad, parent, false);
return new AdsHolder(adView);
} else {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rewards_news_card, parent, false);
return new ItemHolder(itemView);
}
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
CNewsModel pm = models.get(holder.getAdapterPosition());
if (holder instanceof AdsHolder) {
final AdsHolder adsHolder = (AdsHolder) holder;
adsHolder.oNativeExpressAdview.setText("This is AdView");
} else if (holder instanceof ItemHolder) {
final ItemHolder itemHolder = (ItemHolder) holder;
itemHolder.m_newsTitle.setText(pm.getTitle());
itemHolder.m_newsTime.setText(pm.getPub_date());
itemHolder.m_newsHeadline.setText(pm.getDesc());
Picasso.with(mContext).load(pm.getImage_list()).into(itemHolder.m_newsImage, new Callback() {
@Override
public void onSuccess() {
Log.d(TAG, "OnSuccess Called::");
}
@Override
public void onError() {
Log.d(TAG, "OnError Called::");
}
});
}
}
@Override
public int getItemCount() {
return models.size();
}
@Override
public int getItemViewType(int position) {
if (position == 2) {
return 1;
} else {
return 2;
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
private class AdsHolder extends ViewHolder {
private TextView oNativeExpressAdview;
public AdsHolder(View adView) {
super(adView);
oNativeExpressAdview = (TextView) adView.findViewById(R.id.adView);
}
}
private class ItemHolder extends ViewHolder implements View.OnClickListener {
private TextView m_newsTitle, m_newsTime, m_newsHeadline;
private ImageView m_newsImage;
public ItemHolder(View itemView) {
super(itemView);
m_newsTitle = (TextView) itemView.findViewById(R.id.tv_news_header);
m_newsTime = (TextView) itemView.findViewById(R.id.tv_news_time);
m_newsHeadline = (TextView) itemView.findViewById(R.id.tv_news_headline);
m_newsImage = (ImageView) itemView.findViewById(R.id.iv_news_image);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
itemListener.recyclerViewListClicked(v, this.getLayoutPosition());
}
}
}