在 RecyclerAdapter 中每 n 个项目显示广告
Display ad every n items in RecyclerAdapter
我想在我的 RecyclerAdapter 上每 n 个项目显示一个广告。
我很期待这个
|----- Item 0 -----|
|------- AD -------|
|----- Item 1 -----|
|----- Item 2 -----|
|----- Item 3 -----|
|----- Item 4 -----|
|----- Item 5 -----|
|------ ... -------|
|----- Item 15 ----|
|------- AD -------|
|----- Item 16 ----|
|------ ... -------|
但是通过快速滚动我可以得到类似的东西
|----- Item 0 -----|
|------- AD -------|
|----- Item 1 -----|
|----- Item 2 -----|
|----- Item 3 -----|
|------ ... -------|
|---- Item 241 ----|
|------- AD -------|
|------ ... -------|
如果我滚动得很慢,每 15 个项目后就会显示广告。
但如果我快速滚动,广告显示不正确。例如广告显示在商品编号241之后,241 % 15不等于0.
可能是视图重用的问题,我可能做错了,但我不知道怎么办。
这是我的适配器代码。
public class MyCursorRecyclerAdapter extends CursorRecyclerAdapter<RecyclerView.ViewHolder> {
private static final int AD_POS = 15;
private Context mContext;
private LayoutInflater mInflater;
class ViewHolderItem extends RecyclerView.ViewHolder {
public TextView mTextView;
public LinearLayout mLayout;
public ViewHolderItem(View layout) {
super(layout);
mTextView = (TextView) layout.findViewById(R.id.text);
mLayout = (LinearLayout) layout.findViewById(R.id.main_linear_layout);
}
}
public MyCursorRecyclerAdapter(Context context, Cursor c) {
super(c);
mContext = context;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listview_item_layout, parent, false);
return new ViewHolderItem(v);
}
@Override
public void onBindViewHolderCursor(RecyclerView.ViewHolder holderDefault, Cursor cursor) {
int cursorPosition = cursor.getPosition();
final ViewHolderItem holder = (ViewHolderItem) holderDefault;
final String text = cursorPosition + " : "+ cursor.getString(cursor.getColumnIndex(MyTable.MY_COLUMN));
holder.mTextView.setText(text);
final ViewGroup layout = ((ViewHolderItem) holderDefault).mLayout;
if (cursorPosition % AD_POS == 0) {
NativeAd ad = new NativeAd(mContext, "ID");
ad.setAdListener(new AdListener() {
@Override
public void onError(Ad ad, AdError error) {}
@Override
public void onAdLoaded(Ad ad) {
mInflater.inflate(R.layout.native_ad_layout, layout);
AdManager.inflateAd((NativeAd) ad, layout, mContext);
}
@Override
public void onAdClicked(Ad ad) {}
});
ad.loadAd();
} else {
int childCount = layout.getChildCount();
if (layout.getChildAt(childCount-1).getId() == R.id.adUnit) {
layout.removeViewAt(childCount-1);
}
}
}
}
更好的方法是使用 getItemViewType(int position)
方法。
@Override
public int getItemViewType(int position) {
if(cursorPosition % AD_POS == 0){
return 0;
}
return 1;
}
然后您必须在 onCreateViewHolder
方法中使用 viewType 参数,以识别您必须膨胀哪个视图,如下面的代码所示:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType ==0){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listview_item_layout, parent, false);
} else if(viewType ==1){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.different_layout, parent, false);
}
return new ViewHolderItem(v);
}
此外,您必须在 ViewHolderItem
中支持第二个布局或在上述方法中创建 SecondViewHolderItem
和 return 正确的 ViewHolder 并在 [=17] 中转换为正确的 ViewHolder
=] 方法取决于.
的位置
并且您必须在 getItemCount()
方法中考虑这些项目。
我想在我的 RecyclerAdapter 上每 n 个项目显示一个广告。
我很期待这个
|----- Item 0 -----|
|------- AD -------|
|----- Item 1 -----|
|----- Item 2 -----|
|----- Item 3 -----|
|----- Item 4 -----|
|----- Item 5 -----|
|------ ... -------|
|----- Item 15 ----|
|------- AD -------|
|----- Item 16 ----|
|------ ... -------|
但是通过快速滚动我可以得到类似的东西
|----- Item 0 -----|
|------- AD -------|
|----- Item 1 -----|
|----- Item 2 -----|
|----- Item 3 -----|
|------ ... -------|
|---- Item 241 ----|
|------- AD -------|
|------ ... -------|
如果我滚动得很慢,每 15 个项目后就会显示广告。 但如果我快速滚动,广告显示不正确。例如广告显示在商品编号241之后,241 % 15不等于0.
可能是视图重用的问题,我可能做错了,但我不知道怎么办。
这是我的适配器代码。
public class MyCursorRecyclerAdapter extends CursorRecyclerAdapter<RecyclerView.ViewHolder> {
private static final int AD_POS = 15;
private Context mContext;
private LayoutInflater mInflater;
class ViewHolderItem extends RecyclerView.ViewHolder {
public TextView mTextView;
public LinearLayout mLayout;
public ViewHolderItem(View layout) {
super(layout);
mTextView = (TextView) layout.findViewById(R.id.text);
mLayout = (LinearLayout) layout.findViewById(R.id.main_linear_layout);
}
}
public MyCursorRecyclerAdapter(Context context, Cursor c) {
super(c);
mContext = context;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listview_item_layout, parent, false);
return new ViewHolderItem(v);
}
@Override
public void onBindViewHolderCursor(RecyclerView.ViewHolder holderDefault, Cursor cursor) {
int cursorPosition = cursor.getPosition();
final ViewHolderItem holder = (ViewHolderItem) holderDefault;
final String text = cursorPosition + " : "+ cursor.getString(cursor.getColumnIndex(MyTable.MY_COLUMN));
holder.mTextView.setText(text);
final ViewGroup layout = ((ViewHolderItem) holderDefault).mLayout;
if (cursorPosition % AD_POS == 0) {
NativeAd ad = new NativeAd(mContext, "ID");
ad.setAdListener(new AdListener() {
@Override
public void onError(Ad ad, AdError error) {}
@Override
public void onAdLoaded(Ad ad) {
mInflater.inflate(R.layout.native_ad_layout, layout);
AdManager.inflateAd((NativeAd) ad, layout, mContext);
}
@Override
public void onAdClicked(Ad ad) {}
});
ad.loadAd();
} else {
int childCount = layout.getChildCount();
if (layout.getChildAt(childCount-1).getId() == R.id.adUnit) {
layout.removeViewAt(childCount-1);
}
}
}
}
更好的方法是使用 getItemViewType(int position)
方法。
@Override
public int getItemViewType(int position) {
if(cursorPosition % AD_POS == 0){
return 0;
}
return 1;
}
然后您必须在 onCreateViewHolder
方法中使用 viewType 参数,以识别您必须膨胀哪个视图,如下面的代码所示:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType ==0){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listview_item_layout, parent, false);
} else if(viewType ==1){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.different_layout, parent, false);
}
return new ViewHolderItem(v);
}
此外,您必须在 ViewHolderItem
中支持第二个布局或在上述方法中创建 SecondViewHolderItem
和 return 正确的 ViewHolder 并在 [=17] 中转换为正确的 ViewHolder
=] 方法取决于.
并且您必须在 getItemCount()
方法中考虑这些项目。