gpt4 book ai didi

java - 如果 ArrayList 为空,为什么 RecyclerView 没有 header ?

转载 作者:行者123 更新时间:2023-11-29 18:47:52 25 4
gpt4 key购买 nike

我的 RecyclerView 显示 CardViews 和一个标题。Cardviewsjson 获取值,Header 从之前 Activity 中的给定参数获取值。

我的问题是,如果 CardViewjson 不为空,Reyclerview 只显示标题,但如果 json 不向 CardView 发送任何空值,显示 header 。

RecyclerViewAdapter:

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

Context context;

private Activity activity;

List<GetDataAdapter> getDataAdapter;
List<ExerciseAdapter> exerciseAdapter;
private static final int TYPE_HEADER = 0;

private static final int TYPE_ITEM = 1;
public RecyclerViewAdapter(Context context, List<GetDataAdapter> getDataAdapter, List<ExerciseAdapter> exerciseAdapter){

super();

this.exerciseAdapter = exerciseAdapter;
this.getDataAdapter = getDataAdapter;
this.context = context;
}

private GetDataAdapter getItem(int position)
{
return getDataAdapter.get(position);

}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new ViewHolder(itemView);
}
if (viewType == TYPE_HEADER) {
//Inflating header view
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
return new HeaderViewHolder(itemView);
}
return null;
}



class ViewHolder extends RecyclerView.ViewHolder{

public TextView edit;
public TextView NameTextView;
public TextView PhoneNumberTextView;
public TextView SubjectTextView;
public NetworkImageView imageView1;
public NetworkImageView imageView2;
public NetworkImageView imageView3;
public NetworkImageView imageView4;
public NetworkImageView imageView5;
public TextView ratebutton;


public ViewHolder(View itemView) {

super(itemView);
edit = (TextView)itemView.findViewById(R.id.edit);
NameTextView = (TextView) itemView.findViewById(R.id.textView4) ;
PhoneNumberTextView = (TextView) itemView.findViewById(R.id.textView6) ;
SubjectTextView = (TextView) itemView.findViewById(R.id.textView8) ;
imageView1 = (NetworkImageView)itemView.findViewById(R.id.imageView1t);
imageView2 = (NetworkImageView)itemView.findViewById(R.id.imageView2t);
imageView3 = (NetworkImageView)itemView.findViewById(R.id.imageView3t);
imageView4 = (NetworkImageView)itemView.findViewById(R.id.imageView4t);
imageView5 = (NetworkImageView)itemView.findViewById(R.id.imageView5t);
ratebutton = (TextView)itemView.findViewById(R.id.ratebutton);



}
}



@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {

if (position != 0) {
final GetDataAdapter current = getItem(position - 1);


final ViewHolder holder1 = (ViewHolder) holder;
final ImageLoader imageLoader = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader2 = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader3 = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader4 = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader5 = ImageAdapter.getInstance(context).getImageLoader();

imageLoader.get(current.getImageUrl(),
ImageLoader.getImageListener(
holder1.imageView1,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader2.get(current.getImageUrl2(),
ImageLoader.getImageListener(
holder1.imageView2,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader3.get(current.getImageUrl3(),
ImageLoader.getImageListener(
holder1.imageView3,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader4.get(current.getImageUrl4(),
ImageLoader.getImageListener(
holder1.imageView4,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader5.get(current.getImageUrl5(),
ImageLoader.getImageListener(
holder1.imageView5,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);


holder1.imageView1.setImageUrl(current.getImageUrl(), imageLoader);
holder1.imageView2.setImageUrl(current.getImageUrl2(), imageLoader2);
holder1.imageView3.setImageUrl(current.getImageUrl3(), imageLoader3);
holder1.imageView4.setImageUrl(current.getImageUrl4(), imageLoader4);
holder1.imageView5.setImageUrl(current.getImageUrl5(), imageLoader5);
holder1.NameTextView.setText(current.getName());
holder1.PhoneNumberTextView.setText(current.getPhone_number());
holder1.SubjectTextView.setText(current.getSubject2());



} else{
final GetDataAdapter current = getItem(position);
final ViewHolder holder1 = (ViewHolder) holder;
final ImageLoader imageLoader = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader2 = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader3 = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader4 = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader5 = ImageAdapter.getInstance(context).getImageLoader();

imageLoader.get(current.getImageUrl(),
ImageLoader.getImageListener(
holder1.imageView1,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader2.get(current.getImageUrl2(),
ImageLoader.getImageListener(
holder1.imageView2,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader3.get(current.getImageUrl3(),
ImageLoader.getImageListener(
holder1.imageView3,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader4.get(current.getImageUrl4(),
ImageLoader.getImageListener(
holder1.imageView4,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader5.get(current.getImageUrl5(),
ImageLoader.getImageListener(
holder1.imageView5,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);


holder1.imageView1.setImageUrl(current.getImageUrl(), imageLoader);
holder1.imageView2.setImageUrl(current.getImageUrl2(), imageLoader2);
holder1.imageView3.setImageUrl(current.getImageUrl3(), imageLoader3);
holder1.imageView4.setImageUrl(current.getImageUrl4(), imageLoader4);
holder1.imageView5.setImageUrl(current.getImageUrl5(), imageLoader5);
holder1.NameTextView.setText(current.getName());
holder1.PhoneNumberTextView.setText(current.getPhone_number());
holder1.SubjectTextView.setText(current.getSubject2());


}
}
if (holder instanceof HeaderViewHolder) {
final GetDataAdapter current = getItem(position);
final HeaderViewHolder holder1 = (HeaderViewHolder) holder;

//set text of header textviews;



}

}



@Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEADER;
}
return TYPE_ITEM;

}


@Override
public int getItemCount() {
if (!getDataAdapter.isEmpty()) {
return getDataAdapter.size() + 1;
} else

return getDataAdapter.size();

}
private class HeaderViewHolder extends RecyclerView.ViewHolder {
TextView //some Textviews for Header;
ImageView exerciseimage;

public HeaderViewHolder(View view) {
super(view);


// declaring textviews;



}
}




}

@Override
public int getItemCount() {
if (!getDataAdapter.isEmpty()) {
return getDataAdapter.size() + 1;
} else

return getDataAdapter.size();

}

我尝试更改 getDataAdapter 的大小(如果它为空以仅显示标题),但它不起作用。

还有

else{
final GetDataAdapter current = getItem(position);
final ViewHolder holder1 = (ViewHolder) holder;
final ImageLoader imageLoader = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader2 = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader3 = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader4 = ImageAdapter.getInstance(context).getImageLoader();
final ImageLoader imageLoader5 = ImageAdapter.getInstance(context).getImageLoader();

imageLoader.get(current.getImageUrl(),
ImageLoader.getImageListener(
holder1.imageView1,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader2.get(current.getImageUrl2(),
ImageLoader.getImageListener(
holder1.imageView2,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader3.get(current.getImageUrl3(),
ImageLoader.getImageListener(
holder1.imageView3,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader4.get(current.getImageUrl4(),
ImageLoader.getImageListener(
holder1.imageView4,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);
imageLoader5.get(current.getImageUrl5(),
ImageLoader.getImageListener(
holder1.imageView5,
R.mipmap.ic_launcher_image,
android.R.drawable.ic_dialog_alert
)
);


holder1.imageView1.setImageUrl(current.getImageUrl(), imageLoader);
holder1.imageView2.setImageUrl(current.getImageUrl2(), imageLoader2);
holder1.imageView3.setImageUrl(current.getImageUrl3(), imageLoader3);
holder1.imageView4.setImageUrl(current.getImageUrl4(), imageLoader4);
holder1.imageView5.setImageUrl(current.getImageUrl5(), imageLoader5);
holder1.NameTextView.setText(current.getName());
holder1.PhoneNumberTextView.setText(current.getPhone_number());
holder1.SubjectTextView.setText(current.getSubject2());


}
}

我试图在 final GetDataAdapter current = getItem(position); 中更改 position 我认为有必要做一些像这样的 position - 1 或类似的事情:size + 1,因为 Array0 开始计数size1

也许你可以给我提示或解决我的问题。

最佳答案

假设您的列表 getDataAdapter 是空的。然后 getDataAdapter.size() == 0getItemCount() 也返回 0。由于您始终希望显示标题,因此 getItemCount() 应始终至少返回 1。我会简单地重写它:

@Override
public int getItemCount() {
return getDataAdapter.size() + 1;
}

随着这一变化,RecyclerView 列表中始终有第零项。当调用 getItemViewType() 时,它将正确返回第零项的 TYPE_HEADER,因此第零项的 RecyclerView.ViewHolder 将是一个 HeaderViewHolder。 (我知道您说过要在 getDataAdapter 为空时更改其大小,但这似乎是实现此处目标的一种迂回方式。您的数据模型不需要更改以适应您的 View .)

关于 onBindViewHolder():您不需要嵌套的 if-else 语句。由于 header 始终位于 RecyclerView 列表的顶部,因此当 RecyclerView 绑定(bind)到第一个“正常”列表项时,position 参数将为 1。但是第一个普通项对应于getDataAdapter中的第零个项,所以减去1:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {
final ViewHolder viewHolder = (ViewHolder) holder;
final GetDataAdapter current = getItem(position - 1);
//bind to normal views
} else if (holder instanceof HeaderViewHolder) {
final HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
//set text of header textviews
}
}

关于java - 如果 ArrayList 为空,为什么 RecyclerView 没有 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51443133/

25 4 0