Home > Software design >  hide only a view from all recycler view items
hide only a view from all recycler view items

Time:09-13

I have a recycler view item that has few Views. I have a setting that will set the visibility of one Image view after the Recycler view is displayed. What is the best way to achieve this behavior?

in the picture below

What I tried, in the adapter ViewHolder class, I set a boolean that would hide the element and then call notifysetDataChanged()

 public static class ViewHolder extends RecyclerView.ViewHolder {  
    public ImageView imageView;  
     
    public ViewHolder(View itemView) {  
        super(itemView);  
        .....
    }  
    public void bind(item: Model){
        //check and set visibility
        if(showImage) imageview.setVisibility
        else imageview.setVisibility
    }
}  

Calling this bind function from onBindViewHolder.

Is there any better way to achieve this as all the data remain the same only one visibility is changed?

CodePudding user response:

your bind method is fine, but calling notifyDataSetChanged() will make your whole list redraw, all items. it's way better for perfomance to redraw only these items which has changed, use then notifyItemChanged(int position). calling it will make only single onBindViewHolder call with desired position, thus only one desired item will be redrawn (your bind call)

note there are more methods for preventing whole list redraw:

notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)

CodePudding user response:

Are you try this?

android:visibility="gone"
  • Related