gpt4 book ai didi

android - 如何在 android 中的回收器 View 的每个 3rd 索引上放置 View ?

转载 作者:行者123 更新时间:2023-11-30 00:33:37 25 4
gpt4 key购买 nike

我有一个回收器 View ,其中有一些字符串,如 0、1、2、3 等等,在同一个回收器 View 中,我想在每个第 3 个索引上放置一个像 imageview 这样的 View 。下面提到的代码工作正常,但问题是 imageview 从 0 索引开始,如序列:- Imageview-->0,1,2--->imageview--->。这里我不想在索引 0 之前显示 ImageView 。

代码:-

 mList = (RecyclerView) findViewById(R.id.dealList);
layoutManager = new LinearLayoutManager(mContext);
mList.setLayoutManager(layoutManager);
mList.setHasFixedSize(true);
// set divider between items of list
mList.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
list = getResources().getStringArray(R.array.list);
int size = list.length;
ArrayList<String> newList = new ArrayList<>();
for (int i = 0; i < size; i++) {
if (i%2==0){
newList.add(null);
}
newList.add(list[i]);
}
adapter = new CMyAdapter(mContext, newList);
mList.setAdapter(adapter);

适配器代码:-

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);
}
}

最佳答案

在if条件中加入(i!=0)

if (i%2==0 && i!=0){
newList.add(null);
}

关于android - 如何在 android 中的回收器 View 的每个 3rd 索引上放置 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43802230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com