我有一个回收站视图,其中有一些字符串,如0,1,2,3等等,在同一个recyclerview中,我想在每个第三个索引上放置一个像imageview的视图。下面提到的代码工作正常,但问题是imageview从0索引开始,如序列: - Imageview - > 0,1,2 ---> imageview --->。在这里,我不想在索引0之前显示图像视图。
代码: -
private Context mContext;
private ArrayList<String> newList;
private static final int CONTENT_TYPE = 0;
private static final int AD_TYPE = 1;
public CMyAdapter(Context context, ArrayList<String> list) {
this.mContext = context;
this.newList = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder holder = null;
if (viewType == AD_TYPE) {
View adView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view2, parent, false);
holder = new AdViewHolder(adView);
} else {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view1, parent, false);
holder = new ItemViewHolder(itemView);
}
return holder;
}
@Override
public int getItemViewType(int position) {
if (newList.get(position) == null) {
return AD_TYPE;
} else {
return CONTENT_TYPE;
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (newList.get(position) == null) {
AdViewHolder adViewHolder = (AdViewHolder) holder;
adViewHolder.m_dummyImage.setImageResource(R.mipmap.ic_launcher_round);
} else {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.m_Text.setText(newList.get(position));
}
}
@Override
public int getItemCount() {
return newList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View itemView) {
super(itemView);
}
}
private class ItemViewHolder extends ViewHolder {
TextView m_Text;
ItemViewHolder(View itemView) {
super(itemView);
m_Text = (TextView) itemView.findViewById(R.id.textDemo);
}
}
private class AdViewHolder extends ViewHolder {
ImageView m_dummyImage;
AdViewHolder(View v) {
super(v);
m_dummyImage = (ImageView) itemView.findViewById(R.id.adImage);
}
}
适配器的代码: -
self.tblBeerJournal.sectionHeaderHeight = UITableViewAutomaticDimension
self.tblBeerJournal.estimatedSectionHeaderHeight = 160
self.tblBeerJournal.rowHeight = UITableViewAutomaticDimension
self.tblBeerJournal.estimatedRowHeight = UITableViewAutomaticDimension
}
答案 0 :(得分:1)
在条件
中添加(i!= 0)if (i%2==0 && i!=0){
newList.add(null);
}
答案 1 :(得分:0)
您可以在CMyAdapter上执行此操作
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (position%3==0) {
//TODO
} else {
//TODO
}
}
答案 2 :(得分:0)
vector<string> s;
sort(s.begin(),s.end());