简体   繁体   English

仅隐藏所有回收站视图项的视图

[英]hide only a view from all recycler view items

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.我有一个设置,可以在显示 Recycler 视图后设置一个图像视图的可见性。 What is the best way to achieve this behavior?实现这种行为的最佳方法是什么?

在下图中

What I tried, in the adapter ViewHolder class, I set a boolean that would hide the element and then call notifysetDataChanged()我尝试了什么,在适配器 ViewHolder class 中,我设置了一个 boolean 来隐藏元素,然后调用 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.从 onBindViewHolder 调用此绑定 function。

Is there any better way to achieve this as all the data remain the same only one visibility is changed?有没有更好的方法来实现这一点,因为所有数据都保持不变,只有一个可见性发生了变化?

your bind method is fine, but calling notifyDataSetChanged() will make your whole list redraw, all items.您的bind方法很好,但调用notifyDataSetChanged()将使您的整个列表重绘所有项目。 it's way better for perfomance to redraw only these items which has changed, use then notifyItemChanged(int position) .最好只重绘这些已更改的项目,然后使用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)调用它只会使用所需position进行单个onBindViewHolder调用,因此只会重绘一个所需的项目(您的bind调用)

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)

Are you try this?你试试这个?

android:visibility="gone"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM