gpt4 book ai didi

android - 如何在 android 中位置 2 的回收站 View 中添加广告?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:24 26 4
gpt4 key购买 nike

我有一个回收站 View ,我在其中使用 rss 阅读器获取新闻提要,在回收站 View 的索引 2 处,我想放置一个广告 View ,但不知道如何放置。请立即为我提供有关此问题的解决方案。

回收站 View 的代码:-

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

最佳答案

create one layout which holds ad after that in adapter add

@Override
public int getItemViewType(int position) {
switch (position){
case 0:
return 1;
case 1:
return 2;

default:
return 0;
}
}


@Override
public RecyclerAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

switch (viewType) {
case 1:
/*your main item*/
View viewONE = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item, parent, false);

RecyclerAdapter.CustomViewHolder rowONE = new RecyclerAdapter.CustomViewHolder(viewONE);
return rowONE;

case 2:
/** your ad layout*/
View viewTWO = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_layout, parent, false);

RecyclerAdapter.CustomViewHolder rowTWO = new RecyclerAdapter.CustomViewHolder(viewTWO);
return rowTWO;
}
return null;
}


in onBind
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
switch (viewHolder.getItemViewType()) {
case 1:
ItemHolder vh1 = (ItemHolder ) viewHolder;
configureViewHolder1(vh1, i);
break;

case 2:
AdsHolder vh2 = (AdsHolder ) viewHolder;
configureViewHolder2(vh2, i);
break;
}
}

> private void configureViewHolder1(final LoadingVH vh1, int
> position) {
> //change your item data here }
>
>
> private void configureViewHolder2(final ViewHolder2 vh1, int
> position) {
> // change ad data here
> }

关于android - 如何在 android 中位置 2 的回收站 View 中添加广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43840300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com