gpt4 book ai didi

java - RecyclerView Adapter 不会膨胀正确的布局

转载 作者:行者123 更新时间:2023-11-29 22:54:00 26 4
gpt4 key购买 nike

当数组列表 PinnedSongs 为空时,我试图让我的应用程序显示特定布局。

我已经创建了不同的 viewholders 并尝试根据 PinnedSongs.size 来膨胀它们。意思是,如果它为 0,则 onCreateViewHolder 应该展开一个特殊布局。如果不同,则给另一个充气。

public class RecyclerAdapterHome extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public class Viewholder extends RecyclerView.ViewHolder implements View.OnClickListener{

CardView home_cardView;
TextView home_textView;
TextView home_textView_2;
Button home_button;
Button home_button_2;
RelativeLayout parentlayout;
OnNoteListener_2 onNoteListener2;
public Viewholder(@NonNull View itemView, OnNoteListener_2 onNoteListener_2) {
super(itemView);
home_cardView = itemView.findViewById(R.id.cardview_2);
home_textView = itemView.findViewById(R.id.cardview_2_textview);
home_textView_2 = itemView.findViewById(R.id.cardview_2_textview_sub);
home_button = itemView.findViewById(R.id.play_button);
home_button_2 = itemView.findViewById(R.id.push_button);
parentlayout = itemView.findViewById(R.id.home_parentlayout);
home_button.setOnClickListener(this);
this.onNoteListener2 = onNoteListener_2;
}

@Override
public void onClick(View v) {
onNoteListener2.onClick(getAdapterPosition());
}
}
public class Viewholder_2 extends RecyclerView.ViewHolder {
TextView textView_3;
ImageView imageView;
RelativeLayout parentlayout;
public Viewholder_2(@NonNull View itemView) {
super(itemView);
textView_3 = itemView.findViewById(R.id.text_warning);
imageView = itemView.findViewById(R.id.icon);
parentlayout = itemView.findViewById(R.id.home_parentlayout);
}
}

public RecyclerAdapterHome(OnNoteListener_2 mOnNoteListener2, ArrayList<String> pinnedSongs, ArrayList<String> pinnedSongsArtists, Context mContext) {
this.mOnNoteListener2 = mOnNoteListener2;
this.PinnedSongs = pinnedSongs;
this.PinnedSongsArtists = pinnedSongsArtists;
this.mContext = mContext;
}

private ArrayList<String> PinnedSongs = new ArrayList<>();
private ArrayList<String> PinnedSongsArtists = new ArrayList<>();
private Context mContext;
private OnNoteListener_2 mOnNoteListener2;

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = null;
if (PinnedSongs.size()==0){
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.nothing, parent, false);
return new Viewholder_2(v);
}else{
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.homeviewholder,
parent, false);
return new Viewholder(v,mOnNoteListener2 );
}
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (PinnedSongs.size()==0){
Viewholder_2 viewholder_2 = (Viewholder_2) holder;
}else{
Viewholder viewholder = (Viewholder) holder;
viewholder.home_textView.setText(PinnedSongs.get(position));
viewholder.home_textView_2.setText(PinnedSongsArtists.get(position));
}
}

@Override
public int getItemCount() {
return PinnedSongs.size();
}

public interface OnNoteListener_2 {
void onClick(int position);
}
}

到目前为止,当 PinnedSongs 列表为零时,recyclerview 不显示任何内容。但是当它高于零时它会显示一些东西。关于如何修复它的任何想法?

(如果它与此有关,我正在使用 androidx 库)。

最佳答案

它不起作用,因为 getItemCount 方法返回 0 并且 RecyclerView 从不尝试实例化 View 。

意思是当size=0时onBindViewHolder不被调用。

您有不同的选择。其中之一是:

    @Override
public int getItemCount() {
return PinnedSongs.size() > 0 ? PinnedSongs.size() : 1;
}

@Override
public int getItemViewType(int position) {
if (PinnedSongs.size() == 0) {
return EMPTY_VIEW;
}
return super.getItemViewType(position);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;

if (viewType == EMPTY_VIEW) {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.nothing, parent, false);
EmptyViewHolder emptyViewHolder = new EmptyViewHolder(v);
return emptyViewHolder;
}
//normal case
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.homeviewholder, parent, false);
ViewHolder viewholder = new ViewHolder(v);
return viewholder;
}

public class EmptyViewHolder extends RecyclerView.ViewHolder {
public EmptyViewHolder(View itemView) {
super(itemView);
}
}

private static final int EMPTY_VIEW = 1000;

关于java - RecyclerView Adapter 不会膨胀正确的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57612241/

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