如何在 URL 为空时隐藏 ImageView。

huangapple go评论79阅读模式
英文:

How to Imageview hide when url is empty

问题

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());
            
            if (trollModel.getImage() != null && !trollModel.getImage().isEmpty()) {
                trollViewHolder.thrillersimage.setVisibility(View.VISIBLE);
                Picasso.get().load(trollModel.getImage()).into(trollViewHolder.thrillersimage);
            } else {
                trollViewHolder.thrillersimage.setVisibility(View.GONE);
            }

            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);
}
英文:

I have a Imageview its load image from Firebase Database url with Picasso Image loader. I wish to hide imageview when firebase database url is empty. How to set Imageview setVisibility gone in that time.

My code is

        LoadData(categoryId);
}
private void LoadData(String categoryId) {
options = new FirebaseRecyclerOptions.Builder&lt;TrollModel&gt;().setQuery(MCC,TrollModel.class).build();
adapter = new FirebaseRecyclerAdapter&lt;TrollModel, TrollViewHolder&gt;(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(&quot;CategoryId&quot;, 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);
}
}

答案1

得分: 1

在安卓中,每个视图(View)都有一个setVisibility()函数。如果你以正确的方式在谷歌上搜索,你可以找到这个函数。

请参考下面的代码来解决你的问题:

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

There is setVisibility() funciton for every View in android. You can find this if you google it in right way.

Refer below code to solve your problem.

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

huangapple
  • 本文由 发表于 2020年8月23日 11:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63543208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定