0

In my viewHolder, using holder.itemView retrieves the row's contents, so when I use setVisibility() on it, it only hides the contents of the row. How can I hide the whole row instead, by getting a reference to the row's parent view or otherwise?

George
  • 389
  • 5
  • 17
  • `list.remove(position)` followed by `adapter.notifyItemRemoved(position)` – Santanu Sur Feb 21 '20 at 10:51
  • That could work, but this view is directly casted at position == 0 of the recyclerView, hence it is not saved in any list. How would I be able to make it visible/add it back to the recyclerView again afterwards? – George Feb 21 '20 at 10:55
  • The parent view is the `RecyclerView`, you'd have to add your own parent view to do this, and then it'd just be the `itemView` – Ryan M Feb 21 '20 at 10:56
  • 1
    Also there are a bunch of answers here: https://stackoverflow.com/questions/27574805/hiding-views-in-recyclerview – Ryan M Feb 21 '20 at 11:00
  • @RyanMentley thank you, the last link was more than helpful, I used LayoutParams instead to set the visibility AND margin of the row I wanted to show/hide during searching! – George Feb 21 '20 at 11:24

1 Answers1

0

You can hide whole content using Viewholder's rootview. This will hide your whole row

     @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view =
                LayoutInflater.from(parent.getContext(),
                R.layout.row_select_card, parent, false);
        return new MyViewHolder(view);
    }

  private class MyViewHolder extends RecyclerView.ViewHolder {

        private View view;

        private MyViewHolder(View itemView) {
            super(itemView);
           itemview.setVisibility(View.GONE);
        }
}
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
  • Hm okay, it is possible to check for a boolean in my ViewHolder class though (e.g. if isSearchMode then hide that single itemView, else show that single itemView)? – George Feb 21 '20 at 11:33
  • you can show or hide itemview in MyViewHolder class depend on your condition – Jignesh Mayani Feb 21 '20 at 11:46