gpt4 book ai didi

android - 如何使用 CursorAdapter 在 Listview 中实现 Native Express 广告

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:08:05 25 4
gpt4 key购买 nike

我有带有 cursoradapter 的 ListView 。现在我想在 ListView 中实现原生快捷广告。

我已经看到使用简单的 baseAdapter 实现原生广告,因为我们通常使用 List<Object>用于将数据传递给适配器并检查内部项目的类型 getView()添加广告的方法。

 @Override
public View getView(int position, View convertView, ViewGroup parent)
throws IllegalArgumentException {
Object item = getItem(position);

if (item instanceof Listing) {
// Listing items already have all the data required, so they just need to be displayed.
return listingLayout;
} else if (item instanceof AdPlacement) {
return ((AdPlacement) item).getView(convertView, parent);
} else {
// Any unknown items will cause exceptions, though this shouldn't ever happen.
throw new IllegalArgumentException(
String.format("Adapter can't handle getView() for list item of type %s",
item.getClass().getName()));
}


}

如何在 cursoradapter 中检查此条件,因为 cursoradapter 只有带有游标详细信息的方法 newItem()

 @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.list_item_station, parent, false);
ViewHolder holder = new ViewHolder();

v.setTag(holder);
return v;
}

如何在 cursoradapter 中每 10 个项目后添加原生广告

下面是我用来在列表中添加数据的当前代码。

public class StationsCursorAdapter extends CursorAdapter{


public StationsCursorAdapter(Context context) {
super(context, null, true);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.list_item_station, parent, false);
ViewHolder holder = new ViewHolder();

v.setTag(holder);
return v;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.titleTextView.setText(cursor.getString(cursor.getColumnIndex(Station.NAME)));
}

private static final class ViewHolder {
TextView titleTextView;
}

}

最佳答案

创建文件 list_item_native_ad.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="132dp">

<com.google.android.gms.ads.NativeExpressAdView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nativeAd"
ads:adSize="FULL_WIDTHx132"
ads:adUnitId="@string/native_ad_unit_id"/>
</LinearLayout>

在您的适配器中创建 NativeAdViewHelper

public class NativeAdViewHolder extends RecyclerView.ViewHolder {

private final NativeExpressAdView mNativeAd;

public NativeAdViewHolder(View itemView) {
super(itemView);
mNativeAd = (NativeExpressAdView) itemView.findViewById(R.id.nativeAd);
mNativeAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
if (mItemClickListener != null) {
Log.i(TAG, "onAdLoaded");
}
}

@Override
public void onAdClosed() {
super.onAdClosed();
if (mItemClickListener != null) {
Log.i(TAG, "onAdClosed");
}
}

@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
if (mItemClickListener != null) {
Log.i(TAG, "onAdFailedToLoad");
}
}

@Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
if (mItemClickListener != null) {
Log.i(TAG, "onAdLeftApplication");
}
}

@Override
public void onAdOpened() {
super.onAdOpened();
if (mItemClickListener != null) {
Log.i(TAG, "onAdOpened");
}
}
});
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(MainActivity.TEST_DEVICE_ID)
.build();
//You can add the following code if you are testing in an emulator
/*AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();*/
mNativeAd.loadAd(adRequest);
}
}

在您的适配器中覆盖方法 getItemViewType()

@Override
public int getItemViewType(int position) {
if (position>1 && position % 3 == 0) {//in your case replace % 3 with % 10.
return NATIVE_AD_VIEW_TYPE;
}
return DEFAULT_VIEW_TYPE;
}

覆盖方法 getViewTypeCount()

@Override
public int getViewTypeCount() {
return 2;
}

在你的 newView() 方法中

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
switch (viewType) {
default:
view = layoutInflater
.inflate(R.id.content, parent, false);
return new ViewHolder(view);
case NATIVE_AD_VIEW_TYPE:
view = layoutInflater.inflate(R.layout.list_item_native_ad, parent, false);
return new NativeAdViewHolder(view);
}
}

在你的 bindView() 方法中

@Override
public void bindView(View view, Context context, Cursor cursor) {
if (!(holder instanceof ViewHolder)) {
return;
}
holder.titleTextView.setText(cursor.getString(cursor.getColumnIndex(Station.NAME)));
}

希望对您有所帮助!

关于android - 如何使用 CursorAdapter 在 Listview 中实现 Native Express 广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44457931/

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