Rick Joe Rick Joe -4 years ago 205
Android Question

where can I set setVisibility gone to a textview in recyclerview?

I have a recyclerview and in each item I have a button. Sometimes I need to hide this button in some itens.

item 1
button visible

item 2
button gone
...


The problem is, I don't know where is the best place to set
MyButton
as
GONE
. I tried inside
public ViewHolder(View itemView)
but my app crashes.

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

private ImageLoader imageLoader;
private final Context context;

//List to store all posts
List<Posts> Posts;


//Constructor of this class
public CardAdapter(List<Posts> Post, Context context){
super();
this.Posts = Post;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.posts_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

Posts post = Posts.get(position);
holder.post = post;

//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();

//Showing data on the views
holder.imageView.setImageUrl(post.getImageUrl(), imageLoader);
holder.textViewName.setText(post.getName());
holder.textViewPublisher.setText(post.getPublisher());
holder.setIsRecyclable(false);

}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
//Views
public NetworkImageView imageView;
public TextView textViewName;
public TextView textViewPublisher;
public TextView textViewMyButton;
public Posts post;

//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
textViewMyButton = (TextView) itemView.findViewById(R.id.textViewMyButton);

}
@Override
public void onClick(View v) {
//
}

}
}


I want to do something like that:

if(StringName == "1"){
TextView txtView2 = (TextView) itemView.findViewById(R.id.MyButton);
txtView2.setVisibility(View.GONE);
}


any ideas where and how can I add this to hide buttons in recyclerview where StringName==1?

Answer Source
 @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Posts post =  Posts.get(position);
        holder.post = post;

        //Loading image from url
        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();

        //Showing data on the views
        holder.imageView.setImageUrl(post.getImageUrl(), imageLoader);
        if(condition is true){

        holder.textViewName.setVisibility(View.GONE);
        }else{
        holder.textViewName.setVisibility(View.VISIBLE);
        }
            holder.textViewName.setText(post.getName());

  holder.textViewPublisher.setText(post.getPublisher());
            holder.setIsRecyclable(false);

        }

You can set visibility in onBindViewHolder like above. You have to set both visibilities to your view.If you only set it to gone and do not write it to become visible when condition is true.You will encounter errors while scrolling.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download