url 为空时如何隐藏 Imageview

How to Imageview hide when url is empty

我有一个 Imageview,它使用 Picasso 图像加载器从 Firebase 数据库 url 加载图像。我希望在 firebase 数据库 url 为空时隐藏 imageview。那个时候怎么设置Imageview setVisibility消失了。

我的密码是

        LoadData(categoryId);


}

    private void LoadData(String categoryId) {

        options = new FirebaseRecyclerOptions.Builder<TrollModel>().setQuery(MCC,TrollModel.class).build();
        adapter = new FirebaseRecyclerAdapter<TrollModel, TrollViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull TrollViewHolder trollViewHolder, int i, @NonNull TrollModel trollModel) {

                trollViewHolder.thrillername.setText(trollModel.getTitle());
                Picasso.get().load(trollModel.getImage())
                        .into(trollViewHolder.thrillersimage);


                trollViewHolder.setItemClickListner(new ItemClickListner() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {

                Intent detailsIntent = new Intent(TrollActivity.this, TrollDetailActivity.class);
                detailsIntent.putExtra("CategoryId", adapter.getRef(position).getKey());
                startActivity(detailsIntent);


            }
                });

            }

            @NonNull
            @Override
            public TrollViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.troll_items, parent, false);

                return new TrollViewHolder(v);
            }
        };

        adapter.startListening();
        recyclerView.setAdapter(adapter);

    }
}

android中的每个View都有setVisibility()函数。如果你 google 方法正确,你可以找到它。

参考以下代码解决您的问题。

if (trollModel.getImage().isEmpty()) {
   trollViewHolder.thrillersimage.setVisibility(View.GONE);
} else {
   trollViewHolder.thrillersimage.setVisibility(View.VISIBLE);
   Picasso.get().load(trollModel.getImage())
                        .into(trollViewHolder.thrillersimage);
}