Recyclerview 使用两个 viewholder 重叠回收器项目
Recyclerview using two viewholder overlap recycler item
我想在某些布告栏项目后显示 admob 广告。所以我将 recyclerview 更改为使用两个 viewholder 并在位置位于 noticeitem 的末尾时显示 adview。但是它在板项目视图的第一个列表中显示项目并且看不到板的第一个项目
我尝试将第一项添加到位置+1,但它显示了两项
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private RecyclerView recyclerView;
private Context context;
private ArrayList<stTalkListContentsData> talkArrayList = new ArrayList<>();
private ViewGroup parent;
private int mNoticeCount = 0;
public RecyclerAdapter(Context context, RecyclerView recyclerView, ArrayList<stTalkListContentsData> talkArrayList) {
this.context = context;
this.recyclerView = recyclerView;
this.talkArrayList = talkArrayList;
}
public class AdViewHolder extends RecyclerView.ViewHolder {
private AdView adView;
AdViewHolder(View view) {
super(view);
adView = view.findViewById(R.id.adView);
}
}
@Override
public int getItemViewType(int position) {
if (position == mNoticeCount) {
return Define.AD_TYPE;
} else {
return Define.CONTENT_TYPE;
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ViewHolder viewHolder = null;
switch (viewType) {
case Define.CONTENT_TYPE:
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_board_title_list, parent, false);
return new ViewHolder(v);
case Define.AD_TYPE:
default:
View bannerAdView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_board_adview, parent, false);
return new AdViewHolder(bannerAdView);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
int type = getItemViewType(position);
switch (type){
case AD_TYPE:
AdViewHolder bannerHolder = (AdViewHolder) holder;
AdView adView = ((AdViewHolder) holder).adView;
ViewGroup adCardView = (ViewGroup) bannerHolder.itemView;
if (adCardView.getChildCount() > 0) {
adCardView.removeAllViews();
}
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeView(adView);
}
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
adCardView.addView(adView);
break;
case CONTENT_TYPE:{
stTalkListContentsData msgitem = talkArrayList.get(position);
JSAppNoticeContentsData talk_notice_info = msgitem.getTalkNoticeInfo();
JSBoardInfoData talk_board_info = msgitem.getTalkListBoardInfo();
ViewHolder talkholder = (ViewHolder) holder;
....
talkholder.bind(position);
}
break;
}
}
@Override
public int getItemCount() {
return this.talkArrayList.size();
}
}
适配器的getItemCount()函数应该returntalkArrayList.size()+1
@Override
public int getItemCount() {
return this.talkArrayList.size()+1;
}
另外,在 onBindViewHolder 中,您不能直接使用位置来绑定数据,因为 recyclerview 在某个不在 talkArrayList 中的位置保存广告项。
如果position在mNoticeCount之前使用talkArrayList.get(position)获取msgItem或者使用talkArrayList.get(position-1),即
case CONTENT_TYPE:{
stTalkListContentsData msgitem;
if(position < mNoticeCount ){
msgitem = talkArrayList.get(position);
}else {
msgitem = talkArrayList.get(position-1);
}
JSAppNoticeContentsData talk_notice_info = msgitem.getTalkNoticeInfo();
JSBoardInfoData talk_board_info = msgitem.getTalkListBoardInfo();
ViewHolder talkholder = (ViewHolder) holder;
....
talkholder.bind(position);
}
break;
我想在某些布告栏项目后显示 admob 广告。所以我将 recyclerview 更改为使用两个 viewholder 并在位置位于 noticeitem 的末尾时显示 adview。但是它在板项目视图的第一个列表中显示项目并且看不到板的第一个项目
我尝试将第一项添加到位置+1,但它显示了两项
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private RecyclerView recyclerView;
private Context context;
private ArrayList<stTalkListContentsData> talkArrayList = new ArrayList<>();
private ViewGroup parent;
private int mNoticeCount = 0;
public RecyclerAdapter(Context context, RecyclerView recyclerView, ArrayList<stTalkListContentsData> talkArrayList) {
this.context = context;
this.recyclerView = recyclerView;
this.talkArrayList = talkArrayList;
}
public class AdViewHolder extends RecyclerView.ViewHolder {
private AdView adView;
AdViewHolder(View view) {
super(view);
adView = view.findViewById(R.id.adView);
}
}
@Override
public int getItemViewType(int position) {
if (position == mNoticeCount) {
return Define.AD_TYPE;
} else {
return Define.CONTENT_TYPE;
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ViewHolder viewHolder = null;
switch (viewType) {
case Define.CONTENT_TYPE:
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_board_title_list, parent, false);
return new ViewHolder(v);
case Define.AD_TYPE:
default:
View bannerAdView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_board_adview, parent, false);
return new AdViewHolder(bannerAdView);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
int type = getItemViewType(position);
switch (type){
case AD_TYPE:
AdViewHolder bannerHolder = (AdViewHolder) holder;
AdView adView = ((AdViewHolder) holder).adView;
ViewGroup adCardView = (ViewGroup) bannerHolder.itemView;
if (adCardView.getChildCount() > 0) {
adCardView.removeAllViews();
}
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeView(adView);
}
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
adCardView.addView(adView);
break;
case CONTENT_TYPE:{
stTalkListContentsData msgitem = talkArrayList.get(position);
JSAppNoticeContentsData talk_notice_info = msgitem.getTalkNoticeInfo();
JSBoardInfoData talk_board_info = msgitem.getTalkListBoardInfo();
ViewHolder talkholder = (ViewHolder) holder;
....
talkholder.bind(position);
}
break;
}
}
@Override
public int getItemCount() {
return this.talkArrayList.size();
}
}
适配器的getItemCount()函数应该returntalkArrayList.size()+1
@Override
public int getItemCount() {
return this.talkArrayList.size()+1;
}
另外,在 onBindViewHolder 中,您不能直接使用位置来绑定数据,因为 recyclerview 在某个不在 talkArrayList 中的位置保存广告项。 如果position在mNoticeCount之前使用talkArrayList.get(position)获取msgItem或者使用talkArrayList.get(position-1),即
case CONTENT_TYPE:{
stTalkListContentsData msgitem;
if(position < mNoticeCount ){
msgitem = talkArrayList.get(position);
}else {
msgitem = talkArrayList.get(position-1);
}
JSAppNoticeContentsData talk_notice_info = msgitem.getTalkNoticeInfo();
JSBoardInfoData talk_board_info = msgitem.getTalkListBoardInfo();
ViewHolder talkholder = (ViewHolder) holder;
....
talkholder.bind(position);
}
break;