-
森林海
实现此方案的简单方法是,您所要做的就是创建一个同时包含图像和文本的视图,在回收器适配器中检查图像数据是否可用,使图像的可见性可见,否则图像可见性消失。第二种方法是为 创建多个视图。RecyclerView @Override public void onBindViewHolder(ViewHolder holder, int position) { Log.d(TAG, "onBindViewHolder called"); ContentItem item = mContentItems.get(position); if(item.getName()!=null){ holder.textName.setVisibility(View.Visible); holder.textName.setText(item.getName()); }else{ holder.textName.setVisibility(View.GONE); } if(item.getPreviewImageDefault()!=null){ holder.imageIcon.setVisibility(View.Visible) Picasso.with(mContext).load("file://" + item.getPreviewImageDefault()).into(holder.imageIcon); }else{ holder.imageIcon.setVisibility(View.GONE) } }
-
四季花海
另一种可能的解决方案是创建 2 xml 布局,并在回收器视图中使用视图类型。看这个 如何创建回收器视图与多个视图类型?
-
素胚勾勒不出你
如果你想隐藏图像,你可以这样做(支持将可绘制的id作为整数返回):ic_launcherdata.getImage()@Overridepublic void onBindViewHolder(ViewHolder viewHolder, int i) { if(mItems!=null){ AdapterData data = mItems.get(i); viewHolder.text.setText(data.getText()); viewHolder.image.setImageResource(data.getImage()); if(TextUtils.isEmpty(data.getText())){ viewHolder.text.setVisibility(View.GONE); }else{ viewHolder.text.setVisibility(View.VISIBLE); } if(data.getImage()==R.drawable.ic_launcher){ viewHolder.image.setVisibility(View.GONE); }else{ viewHolder.image.setVisibility(View.VISIBLE); } }}
-
慕沐林林
一种可能的解决方案,就像有些人已经说过的那样,是隐藏/显示图像视图。您可以在用于回收站视图的视图管理器中执行此操作。class OptionalImageViewHolder extends RecyclerView.ViewHolder { private ImageView image; private TextView text; // any other views you have public OptionalImageViewHolder(View itemView) { super(itemView); image = itemView.findViewById(R.id.yourImageViewIdHere); text = itemView.findViewById(R.id.yourTextViewIdHere); // same for any other views you have } public void bindView(Tweet tweet) { // This is where the magic happens // Note: I make the assumption that you have a class called "Tweet" // that has a field for "text", a field for "image" (that can be // null if there's no image), and any other necessary fields. text.setText(tweet.getTweetText()); if (tweet.hasImage() /* function that returns whether or not there is an image */) { image.setVisibility(View.VISIBLE); image.setImageBitmap(tweet.getImage()); // or however you are setting the image } else { // else just make the image invisible image.setVisibility(View.GONE); } }}希望这能给你一个想法。
-
慕丝7291255
回收视图支持不同的视图类型(布局),这是在这种情况下的正确方法。例如,class MyAdapter : RecyclerView.Adapter<MyViewHolder>() { override fun getViewTypes(position:Int) = if (mydata[position].hasImage) return R.layout.mylayout_with_image else R.layout.mylayout_no_image; override fun onCreateViewHolder(viewType:Int, parent:ViewGroup) : MyViewHolder = // here viewType = layout id MyViewHolder(layoutInflater.inflate(viewType, parent)) override fun onBindViewHolder(viewHolder:MyViewHolder, position:Int) { // guaranteed viewHolder.itemView is the view you want for that position }}