如果ArrayList为空,为什么不显示RecyclerView没有标题?

时间:2018-07-20 13:00:44

标签: java android android-recyclerview

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

我的问题是,如果Reyclerview的{​​{1}}不为空,而json没有向{{1}发送任何空值,则CardView仅显示标题}显示标题。

RecyclerViewAdapter

json

使用

CardView

我尝试更改 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()); } }  我认为这样做position或诸如此类的final GetDataAdapter current = getItem(position); 是必要的,因为数组从position - 1开始计数,而大小从size + 1开始计数。

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

2 个答案:

答案 0 :(得分:1)

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

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

进行此更改后,RecyclerView列表中始终存在第零个项目。调用getItemViewType()时,它将为该第零项正确返回TYPE_HEADER,因此第零项的RecyclerView.ViewHolder将是HeaderViewHolder。 (我知道您曾说过getDataAdapter为空时更改其大小,但这似乎是在此处实现目标的一种round回方式。您的数据模型无需更改即可容纳您的视图。)< / p>

关于onBindViewHolder():您不需要该嵌套的if-else语句。由于标题始终位于RecyclerView列表的顶部,因此当RecyclerView绑定到第一个“常规”列表项时,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
    }
}

答案 1 :(得分:0)

错误是,如果响应为空,我忘记设置适配器。