6

我有带光标适配器的列表视图。现在我想在 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 个项目后添加原生广告

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

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

}
4

3 回答 3

1

我建议您不要在这种情况下使用游标适配器,因为它效率低下,而是您应该从数据库中获取数据到列表中,并使用 CustomAdapter 扩展 BaseAdapter 来相应地绑定视图。

如果您只需要使用光标适配器,那么您需要修改您的光标数据以在每 10 个位置有一个广告项目。你可以改变你的光标,如:

    public Cursor getModifiedCursorWithAds(Cursor cursor) {
        int size = cursor.getCount();
        MatrixCursor matrixCursor = new MatrixCursor(cursor.getColumnNames());
        int totalAds = size / 10;
        int negativeIds = -1;
        cursor.moveToFirst();
        //assuming only two columns in cursor
        for (int i = 0; i < (size + totalAds); i++) {
            if (i % 10 == 0 && i != 0) {
                matrixCursor.addRow(new String[]{"" + negativeIds--, "Ads data"}); //add dummy data for displaying ads(on the basis of negative ids, ad will be displayed
            } else {
                matrixCursor.addRow(new String[]{cursor.getString(0), cursor.getString(1)}); //add data from actual cursor, if you have more than 2 data modify this statement
                cursor.moveToNext();
            }
        }
        cursor.close();
        return matrixCursor;
    }

修改 newView 以返回不同的广告和其他项目视图,并在 bindView 回调中相应地绑定。

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        int viewType = cursor.getInt(0);
        if (viewType >= 0) {
            view = layoutInflater
                    .inflate(R.id.list_item_content, parent, false);
            ViewHolder holder = new ViewHolder();
            view.setTag(holder);
        } else {
            view = layoutInflater.inflate(R.layout.list_item_native_ad, parent, false);
            AdViewHolder holder = new AdViewHolder();

            view.setTag(holder);
        }
        return view;
    }

我没有测试上面的代码,所以它可能需要一些改变才能使它工作。

于 2017-06-18T12:12:25.917 回答
1

实现视图项类型稍稍不同。您应该定义不同类型的布局和膨胀。看看RecyclerView 项目类型

创建正确的 View Inflating 后,您可以检查 type inOnBind方法,并为每个 View 实现另一种行为。

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder instance of Dog) {
         // Handle one case
    } else {
         // Handle other case
    }
}
于 2017-06-12T11:30:00.113 回答
1

创建文件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)));
    }

希望对您有所帮助!

于 2017-06-16T09:54:09.527 回答