我有一个带有nativeExpressAd的recycleView示例代码。
的活动:
public class RecycleViewAd extends AppCompatActivity {
private Context context;
private RecyclerView mRecyclerView;
// Native Express reklámok és lista elemek listája
private List<Object> mRecyclerViewItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_view_ad);
context = RecycleViewAd.this;
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
// Lista feltöltése
mRecyclerViewItems = new ArrayList<>();
addMenuItems();
// Specify an adapter.
RecyclerView.Adapter adapter = new RecyclerViewAdapter(this, mRecyclerViewItems);
mRecyclerView.setAdapter(adapter);
}
这是适配器:
class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1;
private final Context mContext;
private final List<Object> mRecyclerViewItems;
public RecyclerViewAdapter(Context context, List<Object> recyclerViewItems) {
this.mContext = context;
this.mRecyclerViewItems = recyclerViewItems;
}
public static class MenuItemViewHolder extends RecyclerView.ViewHolder {
private TextView menuItemName;
private TextView menuItemDescription;
private TextView menuItemPrice;
private TextView menuItemCategory;
private ImageView menuItemImage;
private CardView row;
MenuItemViewHolder(View view) {
super(view);
menuItemImage = (ImageView) view.findViewById(R.id.menu_item_image);
menuItemName = (TextView) view.findViewById(R.id.menu_item_name);
menuItemPrice = (TextView) view.findViewById(R.id.menu_item_price);
menuItemCategory = (TextView) view.findViewById(R.id.menu_item_category);
menuItemDescription = (TextView) view.findViewById(R.id.menu_item_description);
row = (CardView) view.findViewById(R.id.menu_item_card_view);
}
}
public static class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {
NativeExpressAdView adView;
NativeExpressAdViewHolder(View view) {
super(view);
adView = (NativeExpressAdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("B9EE0FBCF9F3B7065A60A390FC73594D")
.build();
adView.loadAd(adRequest);
}
}
@Override
public int getItemCount() {
return mRecyclerViewItems.size();
}
@Override
public int getItemViewType(int position) {
return (position % mContext.getResources().getInteger(R.integer.items_per_ad) == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE
: MENU_ITEM_VIEW_TYPE;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case MENU_ITEM_VIEW_TYPE:
View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.menu_item_container, viewGroup, false);
viewHolder = new MenuItemViewHolder(menuItemLayoutView);
break;
case NATIVE_EXPRESS_AD_VIEW_TYPE:
View nativeExpressLayoutView = LayoutInflater.from(
viewGroup.getContext()).inflate(R.layout.native_express_ad_container,
viewGroup, false);
viewHolder = new NativeExpressAdViewHolder(nativeExpressLayoutView);
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case MENU_ITEM_VIEW_TYPE:
MenuItemViewHolder menuItemHolder = (MenuItemViewHolder) holder;
MenuItem menuItem = (MenuItem) mRecyclerViewItems.get(position);
// Get the menu item image resource ID.
String imageName = menuItem.getImageName();
int imageResID = mContext.getResources().getIdentifier(imageName, "drawable",
mContext.getPackageName());
// Add the menu item details to the menu item view.
menuItemHolder.menuItemImage.setImageResource(imageResID);
menuItemHolder.menuItemName.setText(menuItem.getName());
menuItemHolder.menuItemPrice.setText(menuItem.getPrice());
menuItemHolder.menuItemCategory.setText(menuItem.getCategory());
menuItemHolder.menuItemDescription.setText(menuItem.getDescription());
break;
case NATIVE_EXPRESS_AD_VIEW_TYPE:
break;
}
}
你能帮我解决一下我做错了什么,因为使用这个解决方案时,当adView进入列表时,回收视图会滞后。 没有广告就很顺利。
谢谢!