Disable click/touch for some of a RecyclerView's items

前端 未结 1 1613
遥遥无期
遥遥无期 2021-01-29 07:29

Is there a way to prevent clicking in a specific item of a recycler view? Already tried to set the view as not clickable and not enabled in the view holder constructor but still

1条回答
  •  Happy的楠姐
    2021-01-29 08:10

    Probably the easiest way to completely block interaction with anything inside a single item is to put a transparent view over it that intercepts all touch events. You'd do this by wrapping your existing itemView layout in a FrameLayout and adding another view on top of that:

    
    
        
    
        
    
    
    

    Inside onCreateViewHolder(), you can assign a no-op click listener to the overlay:

    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.itemview, parent, false);
        MyViewHolder holder = new MyViewHolder(itemView);
    
        holder.overlay.setOnClickListener(v -> {});
    
        return holder;
    }
    

    Now, when you want to disable clicks, you can call

    holder.overlay.setVisibility(View.VISIBLE);
    

    and when you want to disable them, you can call

    holder.overlay.setVisibility(View.GONE);
    

    0 讨论(0)
提交回复
热议问题