我有一个包含所有文字和图片的布局文件。我想留下一些文本和图像的空白,但也删除它从布局文件中占用的空间。所以我使用setVisibility并使用TextUtils.isEmpty删除文本,如果它的空白("")
我不确定如何删除图像空间。每次我不想要图片时都可以使用ic_launcher,只要它匹配ic_launcher setVisibility(GONE)
public class BasicAdapter extends RecyclerView.Adapter<BasicAdapter.ViewHolder> {
List<AdapterData> mItems;
public BasicAdapter() {
super();
mItems = new ArrayList<>();
AdapterData data = new AdapterData();
data.setText("dummy text");
data.setImage(R.drawable.image_one);
mItems.add(data);
data = new AdapterData();
data.setText("");
data.setImage(R.drawable.ic_launcher);
mItems.add(data);
data = new AdapterData();
data.setText("");
data.setImage(R.drawable.image_two);
mItems.add(data);
data = new AdapterData();
data.setText("dummy text");
data.setImage(R.drawable.ic_launcher);
mItems.add(data);
data = new AdapterData();
data.setText("dummy text");
data.setImage(R.drawable.image_three);
mItems.add(data);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.test2, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
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);
}
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView text;
public ImageView image;
public ViewHolder(View itemView) {
super(itemView);
text = (TextView)itemView.findViewById(R.id.textView1);
image = (ImageView)itemView.findViewById(R.id.imageView1);
}
}
}