2016-11-21 235 views
1

一)我想告訴從裏面查詢火力與火力地堡查詢實施火力地堡本土快遞廣告

B)我用PostListFragment.java顯示帖子生成的動態列表AdMob原生廣告明確和職位適配器RVAdapter。 java的

C)加載和編程顯示廣告和它正常工作與靜態列表

d),這是動態列表:

mPosts = getPosts(postsQuery); 

F),這是靜態列表:

mPosts = new ArrayList<>(); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 

的錯誤是: - 當我使用靜態列表,它工作正常,沒有崩潰,我得到了NativeExpressAd每8個位置,但是當我更換這個靜態列表動態列表(來自火力查詢來)崩潰,因爲鑄造的問題,而列表大小爲零,即使有屏幕

這裏所顯示的帖子是我的適配器代碼:

package com.google.firebase.quickstart.database; 

import android.content.Context; 
import android.content.Intent; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import com.google.android.gms.ads.NativeExpressAdView; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.MutableData; 
import com.google.firebase.database.Transaction; 
import com.google.firebase.quickstart.database.fragment.PostListFragment; 
import com.google.firebase.quickstart.database.models.Post; 
import com.google.firebase.quickstart.database.viewholder.NativeExpressAdViewHolder; 
import com.google.firebase.quickstart.database.viewholder.PostViewHolder; 
import java.util.List; 

public class RVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    private static final int POST = 0; 
    private static final int AD = 1; 

    Context context; 
    List<Object> mPosts; 
    DatabaseReference mDatabase; 

    public RVAdapter(Context context, List<Object> mPosts , DatabaseReference mDatabase) { 
     this.context = context; 
     this.mPosts = mPosts; 
     this.mDatabase = mDatabase; 
    } 


    @Override 
    public int getItemCount() { 
     return mPosts.size(); 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return (position % PostListFragment.ITEMS_PER_AD == 0) ? AD : POST; 
    } 


    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 
     switch (viewType) { 
      case POST: 
       View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
         R.layout.item_post, viewGroup, false); 
       return new PostViewHolder(menuItemLayoutView); 
      case AD: 
       // fall through 
      default: 
       View nativeExpressLayoutView = LayoutInflater.from(
         viewGroup.getContext()).inflate(R.layout.item_ad, 
         viewGroup, false); 
       return new NativeExpressAdViewHolder(nativeExpressLayoutView); 
     } 

    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     int viewType = getItemViewType(position); 
     switch (viewType) { 
      case POST: 
       configurePost(holder , position); 
       break; 
      case AD: 
       // fall through 
      default: 
       configureAd(holder , position); 
     } 
    } 

    public void configureAd(RecyclerView.ViewHolder holder , int position){ 
     NativeExpressAdViewHolder nativeExpressHolder = (NativeExpressAdViewHolder) holder; 
     NativeExpressAdView adView = (NativeExpressAdView) mPosts.get(position); 
     ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView; 
     if (adCardView.getChildCount() > 0) { 
      adCardView.removeAllViews(); 
     } 
     // Add the Native Express ad to the native express ad view. 
     adCardView.addView(adView); 
    } 

    public void configurePost (RecyclerView.ViewHolder holder , int position){ 
     final PostViewHolder viewHolder = (PostViewHolder) holder; 
     final Post model = (Post) mPosts.get(position); 
     final String postKey = model.key; 
     viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Launch PostDetailActivity 
       Intent intent = new Intent(context, PostDetailActivity.class); 
       intent.putExtra(PostDetailActivity.EXTRA_POST_KEY, postKey); 
       context.startActivity(intent); 
      } 
     }); 

     // Determine if the current user has liked this post and set UI accordingly 
     if (model.stars.containsKey(getUid())) { 
      viewHolder.starView.setImageResource(R.drawable.ic_toggle_star_24); 
     } else { 
      viewHolder.starView.setImageResource(R.drawable.ic_toggle_star_outline_24); 
     } 

     // Bind Post to ViewHolder, setting OnClickListener for the star button 
     viewHolder.bindToPost(model, new View.OnClickListener() { 
      @Override 
      public void onClick(View starView) { 
       // Need to write to both places the post is stored 
       DatabaseReference globalPostRef = mDatabase.child("posts").child(postKey); 
       DatabaseReference userPostRef = mDatabase.child("user-posts").child(model.uid).child(postKey); 

       // Run two transactions 
       onStarClicked(globalPostRef); 
       onStarClicked(userPostRef); 
      } 
     }); 

    } 

    private void onStarClicked(DatabaseReference postRef) { 
     postRef.runTransaction(new Transaction.Handler() { 
      @Override 
      public Transaction.Result doTransaction(MutableData mutableData) { 
       Post p = mutableData.getValue(Post.class); 
       if (p == null) { 
        return Transaction.success(mutableData); 
       } 

       if (p.stars.containsKey(getUid())) { 
        // Unstar the post and remove self from stars 
        p.starCount = p.starCount - 1; 
        p.stars.remove(getUid()); 
       } else { 
        // Star the post and add self to stars 
        p.starCount = p.starCount + 1; 
        p.stars.put(getUid(), true); 
       } 

       // Set value and report transaction success 
       mutableData.setValue(p); 
       return Transaction.success(mutableData); 
      } 

      @Override 
      public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) { 
       // Transaction completed 
      } 
     }); 
    } 

    public String getUid() { 
     return FirebaseAuth.getInstance().getCurrentUser().getUid(); 
    } 

} 

這裏片段代碼:

package com.google.firebase.quickstart.database.fragment; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Toast; 
import com.google.android.gms.ads.AdListener; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdSize; 
import com.google.android.gms.ads.NativeExpressAdView; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import com.google.firebase.database.Query; 
import com.google.firebase.database.ValueEventListener; 
import com.google.firebase.quickstart.database.R; 
import com.google.firebase.quickstart.database.RVAdapter; 
import com.google.firebase.quickstart.database.models.Post; 
import java.util.ArrayList; 
import java.util.List; 

public abstract class PostListFragment extends Fragment { 

    private DatabaseReference mDatabase; 

    private RVAdapter mAdapter; 
    private RecyclerView mRecycler; 
    private LinearLayoutManager mManager; 

    public static final int ITEMS_PER_AD = 8; 
    private static final int NATIVE_EXPRESS_AD_HEIGHT = 150; 
    private static final int NATIVE_EXPRESS_AD_WIDTH = 320; 
    private static final String AD_UNIT_ID = "ca-app-pub-7651927944958903/3704936407"; 

    private List<Object> mPosts; 

    public PostListFragment() {} 

    @Override 
    public View onCreateView (LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
     super.onCreateView(inflater, container, savedInstanceState); 
     View rootView = inflater.inflate(R.layout.fragment_all_posts, container, false); 

     mDatabase = FirebaseDatabase.getInstance().getReference(); 

     mRecycler = (RecyclerView) rootView.findViewById(R.id.messages_list); 
     mRecycler.setHasFixedSize(true); 

     return rootView; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     // Set up Layout Manager, reverse layout 
     mManager = new LinearLayoutManager(getActivity()); 
     mManager.setReverseLayout(true); 
     mManager.setStackFromEnd(true); 
     mRecycler.setLayoutManager(mManager); 


     Query postsQuery = getQuery(mDatabase); 

     mPosts = getPosts(postsQuery); 

//  mPosts = new ArrayList<>(); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 
//  mPosts.add(new Post("key" , "uid" , "author" , "title" , "body")); 


     addNativeExpressAds(); 
     setUpAndLoadNativeExpressAds(); 

     mAdapter = new RVAdapter(getActivity() ,mPosts , mDatabase); 

     //this toast always gets 0 posts or 1 ad 
     Toast.makeText(getActivity(), mPosts.size()+"", Toast.LENGTH_SHORT).show(); 

     mRecycler.setAdapter(mAdapter); 

    } 


    public String getUid() { 
     return FirebaseAuth.getInstance().getCurrentUser().getUid(); 
    } 

    public abstract Query getQuery(DatabaseReference databaseReference); 

    public List<Object> getPosts(Query query){ 
     final List<Object> items = new ArrayList<>(); 
     query.getRef().addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { 
        Post post = postSnapshot.getValue(Post.class); 
        items.add(post); 
       } 
      } 
      @Override 
      public void onCancelled(DatabaseError error) { 
       // Failed to read value 
       Toast.makeText(getActivity(), "Failed to load posts!", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     return items; 
    } 

    private void addNativeExpressAds() { 

     // Loop through the items array and place a new Native Express ad in every ith position in 
     // the items List. 
     for (int i = 0; i <= mPosts.size(); i += ITEMS_PER_AD) { 
      final NativeExpressAdView adView = new NativeExpressAdView(getActivity()); 
      mPosts.add(i, adView); 

     } 
    } 

    private void setUpAndLoadNativeExpressAds() { 
     // Use a Runnable to ensure that the RecyclerView has been laid out before setting the 
     // ad size for the Native Express ad. This allows us to set the Native Express ad's 
     // width to match the full width of the RecyclerView. 
     mRecycler.post(new Runnable() { 
      @Override 
      public void run() { 
//    final float density = getActivity().getResources().getDisplayMetrics().density; 
       // Set the ad size and ad unit ID for each Native Express ad in the items list. 
       for (int i = 0; i <= mPosts.size(); i += ITEMS_PER_AD) { 
        final NativeExpressAdView adView = (NativeExpressAdView) mPosts.get(i); 
        AdSize adSize = new AdSize(NATIVE_EXPRESS_AD_WIDTH, NATIVE_EXPRESS_AD_HEIGHT); 
        adView.setAdSize(adSize); 
        adView.setAdUnitId(AD_UNIT_ID); 
       } 

       // Load the first Native Express ad in the items list. 
       loadNativeExpressAd(ITEMS_PER_AD); 
      } 
     }); 
    } 

    private void loadNativeExpressAd(final int index) { 

     if (index >= mPosts.size()) { 
      return; 
     } 

     Object item = mPosts.get(index); 
     if (!(item instanceof NativeExpressAdView)) { 
      throw new ClassCastException("Expected item at index " + index + " to be a Native" 
        + " Express ad."); 
     } 

     final NativeExpressAdView adView = (NativeExpressAdView) item; 

     // Set an AdListener on the NativeExpressAdView to wait for the previous Native Express ad 
     // to finish loading before loading the next ad in the items list. 
     adView.setAdListener(new AdListener() { 
      @Override 
      public void onAdLoaded() { 
       super.onAdLoaded(); 
       // The previous Native Express ad loaded successfully, call this method again to 
       // load the next ad in the items list. 
       loadNativeExpressAd(index + ITEMS_PER_AD); 
      } 

      @Override 
      public void onAdFailedToLoad(int errorCode) { 
       // The previous Native Express ad failed to load. Call this method again to load 
       // the next ad in the items list. 
       Log.e("MainActivity", "The previous Native Express ad failed to load. Attempting to" 
         + " load the next Native Express ad in the items list."); 
       loadNativeExpressAd(index + ITEMS_PER_AD); 
      } 
     }); 

     // Load the Native Express ad. 
     adView.loadAd(new AdRequest.Builder().build()); 
    } 
} 

這裏是堆棧跡:

11-21 14:51:47.182 21965-21965/com.google.firebase.quickstart.database W/dalvikvm:線程ID = 1:螺紋與未捕獲的異常退出(group = 0x417f89a8) 11-21 14:51:47.182 21965-21965/com.google.firebase.quickstart.database W/dalvikvm:threadid = 1:發生未捕獲異常 11-21 14:51:47.182 21965-21965 /com.google.firebase.quickstart.database W/System.err:java.lang.ClassCastException:com.google.firebase.quickstart.database.models.Post無法轉換爲com.google.android.gms.ads.NativeExpressAdView 11-21 14:51:47.186 21965-21965/com.google。 firebase.quickstart.database W/System.err:com.google.firebase.quickstart.database.RVAdapter.configureAd(RVAdapter.java:83) 11-21 14:51:47.186 21965-21965/com.google.firebase .quickstart.database W/System.err:com.google.firebase.quickstart.database.RVAdapter.onBindViewHolder(RVAdapter.java:77) 11-21 14:51:47.187 21965-21965/com.google.firebase。 quickstart.database W/System.err:at android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:5825) 11-21 14:51:47.187 21965-21965/com.google.firebase.quickstart .database W/System.err:at android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:5858) 11-21 14:51:47.187 21965-21965/com.google.firebase.quickstart。數據庫W/System.err:在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5094) 11-21 14:51:47.187 21965-21965/com.google.firebase.quickstart.database W/System.err:at android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4970) 11-21 14 :51:47.187 21965-21965/com.google.firebase.quickstart.database W/System.err:at android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2029) 11-21 14: 51:47.188 21965-21965/com.google.firebase.quickstart.database W/System.err:at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414) 11-21 14:51:47.188 21965-21965/com.google.firebase.quickstart.database W/System.err:at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377) 11-21 14:51:47.188 21965-21965 /com.google.firebase.quickstart.database W/System.err:位於android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager。java:1193) 11-21 14:51:47.188 21965-21965/com.google.firebase.quickstart.database W/System.err:at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1043 ) 11-21 14:51:47.189 21965-21965/com.google.firebase.quickstart.database W/System.err:at android.support.v7.widget.RecyclerView $ ViewFlinger.run(RecyclerView.java:4413) 11-21 14:51:47.189 21965-21965/com.google.firebase.quickstart.database W/System.err的:在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:776) 11-21 14 :51:47.189 21965-21965/com.google.firebase.quickstart.database W/System.err:at android.view.Choreographer.doCallbacks(Choreographer.java:579) 11-21 14:51:47.189 21965-21965 /com.google.firebase.quickstart.database W/System.err:at android.view.Choreographer.doFrame(Choreographer.java:547) 11-21 14:51:47.189 21965-21965/com.google.firebase.quickstart.database W/System.err:at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:762) 11-21 14 :51:47.189 21965-21965/com.google.firebase.quickstart.database W/System.err:at android.os.Handler.handleCallback(Handler.java:800) 11-21 14:51:47.190 21965-21965 /com.google.firebase.quickstart.database W/System.err:at android.os.Handler.dispatchMessage(Handler.java:100) 11-21 14:51:47.190 21965-21965/com.google.firebase。 quickstart.database W/System.err:at android.os.Looper.loop(Looper.java:194) 11-21 14:51:47.190 21965-21965/com.google.firebase.quickstart.database W/System。 err:at android.app.ActivityThread.main(ActivityThread.java:5391) 11-21 14:51:47.191 21965-21965/com.google.firebase.quickstart.database W/System.err:at java.la ng.reflect.Method.invokeNative(Native Method) 11-21 14:51:47.191 21965-21965/com.google.firebase.quickstart.database W/System.err:at java.lang.reflect.Method.invoke( Method.java:525) 11-21 14:51:47.191 21965-21965/com.google.firebase.quickstart.database W/System.err的:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit .java:833) 11-21 14:51:47.191 21965-21965/com.google.firebase.quickstart.database W/System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 600) 11-21 14:51:47.192 21965-21965/com.google.firebase.quickstart.database W/System.err的:在dalvik.system.NativeStart.main(本機方法) 11-21 14時51分: 47.192 21965-21965/com.google.firebase.quickstart.database W/dalvikvm:threadid = 1:調用UncaughtExceptionHandler 11-21 14:51:47.196 21965-21965/com.google。 firebase.quickstart.database E/AndroidRuntime:致命異常:主要 java.lang.ClassCastException:com.google.firebase.quickstart.database.models.Post不能轉換到com.google.android.gms.ads.NativeExpressAdView 在com.google.firebase.quickstart.database.RVAdapter.configureAd(RVAdapter.java:83) 在com.google.firebase.quickstart.database.RVAdapter.onBindViewHolder(RVAdapter.java:77) 在android.support.v7。 widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:5825) at android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:5858) at android.support.v7.widget.RecyclerView $ Recycler。 getViewForPosition(RecyclerView.java:5094) at android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4970) at android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2029) at android.support。在android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager。)上的android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377) 上的v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414) 。 Java的:1193) 在android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1043) 在android.support.v7.widget.RecyclerView $ ViewFlinger。運行(RecyclerView.java:4413) at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:776) at android.view.Choreographer.doCallbacks(Choreographer.java:579) at android.view.Choreographer。在android.os.Handler上的android.os.Handler.handleCallback(Handler.java:800) 上的doFrame(Choreographer.java:547) $ FrameDisplayEventReceiver.run(Choreographer.java:762) 。 dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5391) 在java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit。 Java的:833) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 在dalvik.system.NativeStart.main(本機方法)

回答

0

我去的其他方式,並加入廣告「onDataChange」(解決方法就是在科特林):

Ref.addListenerForSingleValueEvent(object : ValueEventListener { 
      override fun onCancelled(p0: DatabaseError?) { 
       Log.v(TAG, "empty") 
      } 

      override fun onDataChange(p0: DataSnapshot) { 
       val items = p0.childrenCount.toInt() 
       var i = 0 
       for (ds in p0.children) { 
        if (position % ITEMS_PER_AD == 0) { 
         val adView = NativeExpressAdView(activity) 
         val scale = activity.resources.displayMetrics.density 
         val adSize = AdSize((activity.resources.displayMetrics.widthPixels/scale).toInt(), NATIVE_EXPRESS_AD_HEIGHT) 
         adView.adSize = adSize 
         adView.adUnitId = "my unit id..." 
         adView.loadAd(AdRequest.Builder().build()) 
         list.add(position, adView) 
         position += 1 
        } 
        val model = ds.getValue(someModel::class.java) 
        list.add(position, model!!) 
        i += 1 
        position += 1 
        if (i == items) { 
         val adapter = RecyclerViewAdapter(activity, list) 
         mRecycler.adapter = adapter 
        } 
       } 
      } 
     }) 

這是我的適配器:

class RecyclerViewAdapter(private val mContext: Context, private val mRecyclerViewItems: List<Any>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { 
    inner class MenuItemViewHolder internal constructor(view: View) : RecyclerView.ViewHolder(view) { 
     val menuItemName: TextView 

     init { 
      menuItemName = view.findViewById(R.id.menu_item_name) as TextView 
     } 
    } 

    inner class NativeExpressAdViewHolder internal constructor(view: View) : RecyclerView.ViewHolder(view) 

    override fun getItemCount(): Int { 
     return mRecyclerViewItems.size 
    } 

    override fun getItemViewType(position: Int): Int { 
     return if (position % ITEMS_PER_AD == 0) 
      NATIVE_EXPRESS_AD_VIEW_TYPE 
     else 
      MENU_ITEM_VIEW_TYPE 
    } 

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder { 
     when (viewType) { 
      MENU_ITEM_VIEW_TYPE -> { 
       val menuItemLayoutView = LayoutInflater.from(viewGroup.context).inflate(
         R.layout.menu_item_container, viewGroup, false) 
       return MenuItemViewHolder(menuItemLayoutView) 
      } 
      else -> { 
       val nativeExpressLayoutView = LayoutInflater.from(
         viewGroup.context).inflate(R.layout.native_express_ad_container, 
         viewGroup, false) 
       return NativeExpressAdViewHolder(nativeExpressLayoutView) 
      } 
     } 

    } 

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { 
     val viewType = getItemViewType(position) 
     when (viewType) { 
      MENU_ITEM_VIEW_TYPE -> { 
       val menuItemHolder = holder as MenuItemViewHolder 
       val menuItem = mRecyclerViewItems[position] as modelWOD 
       menuItemHolder.menuItemName.text = menuItem.name 
      } 
      else -> { 
       val nativeExpressHolder = holder as NativeExpressAdViewHolder 
       val adView = mRecyclerViewItems[position] as NativeExpressAdView 
       val adCardView = nativeExpressHolder.itemView as ViewGroup 
       if (adCardView.childCount > 0) adCardView.removeAllViews() 
       if (adView.parent != null) (adView.parent as ViewGroup).removeView(adView) 
       adCardView.addView(adView) 
      } 
     } 
    } 

    companion object { 
     private val MENU_ITEM_VIEW_TYPE = 0 
     private val NATIVE_EXPRESS_AD_VIEW_TYPE = 1 
     private val ITEMS_PER_AD = 8 
    } 
}