如何将NativeExpressAdView集成到FirebaseRecyclerAdapter?

时间:2017-05-08 16:58:42

标签: android firebase firebase-realtime-database firebaseui

我想知道是否有将NativeAdExpress集成到FirebaseRecyclerAdapter(来自FirebaseUI)的好方法? 我正在努力解决这个问题,如何将NativeExpressAdView与FirebaseArray容器连接起来。有任何样品或建议吗?

1 个答案:

答案 0 :(得分:0)

我将不得不创建两个视图持有者,下面显示了几个小例子:

1)NativeAdViewHolder

2)BlogViewHolder

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("AndroidBash", "onAdLoaded");
                }
            }

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

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

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

            @Override
            public void onAdOpened() {
                super.onAdOpened();
                if (mItemClickListener != null) {
                    Log.i("AndroidBash", "onAdOpened");
                }
            }
        });
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(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);
    }
}



// RecyclerView class
public static class BlogViewHolder extends RecyclerView.ViewHolder{

    View mView;
    ImageButton mLikeBtn,mShareBtn;

    DatabaseReference mDatabaseLike;
    FirebaseAuth mAuth;
    public BlogViewHolder(View itemView) {
        super(itemView);
        mView  = itemView;
        mLikeBtn = (ImageButton)mView.findViewById(R.id.like_btn);
        mShareBtn = (ImageButton)mView.findViewById(R.id.share_post_btn);

    }

    public void setTitle(String title){

        TextView post_title = (TextView)mView.findViewById(R.id.post_title);
        post_title.setText(title);
    }

    public void setUsername(String username){
        TextView post_username = (TextView)mView.findViewById(R.id.post_username);
        post_username.setText("By: "+username);
    }

    public void setDate(String date){
        TextView etDate = (TextView)mView.findViewById(R.id.today_post_time);
        etDate.setText("Date: "+date);
    }

}

然后

  @Override
                public RecyclerView.ViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view;
                    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
                    switch (viewType) {
                        default:
                            view = layoutInflater
                                    .inflate(R.layout.today_news_blog_row, parent, false);
                            return new BlogViewHolder(view);
                        case NATIVE_AD_VIEW_TYPE:
                            view = layoutInflater.inflate(R.layout.list_item_native_ad, parent, false);
                            return new NativeAdViewHolder(view);
                    }
                }
            };