title: How to create selectable tags with RecyclerView?
body: <p>I want to create tags using RecyclerView that can be selected to produce results.</p>
  <p><a href="https://i.stack.imgur.com/54a20.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/54a20.jpg" alt="enter image description here" /></a></p>
  <p>What I want to achieve is when I tap on Books TextView on the top-right must change to Books immediately. In my case when I tap on Books it just stays in Art and it replaces Art with Books only when I replace my fragments. I also highlight buttons when clicked (changing border from grey to black) but after changing fragments highlights return back to Art again. Shortly I want to highlight button when clicked and change TextView content based on clicked button text. I partly achieved it by using interfacews but didn't get what I wanted. I provided codes below:</p>
  <p>TrendCategoryTagsAdapter.java:</p>
  <pre><code>public class TrendCategoryTagsAdapter extends FirestoreRecyclerAdapter&lt;CategorySelection, TrendCategoryTagsAdapter.TrendCategoryTagsHolder&gt; {
  
      Context context;
      onCategoryTagClicked onCategoryTagClicked;
  
      int row_index;
  
      public TrendCategoryTagsAdapter(@NonNull FirestoreRecyclerOptions&lt;CategorySelection&gt; options, Context context, com.rajabmammadli.paragrafredesign.Interface.onCategoryTagClicked onCategoryTagClicked) {
          super(options);
          this.context = context;
          this.onCategoryTagClicked = onCategoryTagClicked;
      }
  
      @Override
      protected void onBindViewHolder(@NonNull final TrendCategoryTagsHolder holder, final int position, @NonNull CategorySelection model) {
          holder.categoryNameText.setText(model.getCategoryName());
  
          holder.categoryNameContainer.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  row_index = position;
                  notifyDataSetChanged();
              }
          });
  
          if (row_index == position) {
              holder.categoryNameContainer.setBackground(ContextCompat.getDrawable(context, R.drawable.black_rounded_bg));
              onCategoryTagClicked.onTagClick(holder.categoryNameText.getText().toString());
          } else {
              holder.categoryNameContainer.setBackground(ContextCompat.getDrawable(context, R.drawable.grey_rounded_bg));
          }
      }
  
      @NonNull
      @Override
      public TrendCategoryTagsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(context).inflate(R.layout.trendcategory_cell, parent, false);
          return new TrendCategoryTagsAdapter.TrendCategoryTagsHolder(v);
      }
  
      public static class TrendCategoryTagsHolder extends RecyclerView.ViewHolder {
          RelativeLayout categoryNameContainer;
          TextView categoryNameText;
  
          public TrendCategoryTagsHolder(@NonNull View itemView) {
              super(itemView);
              categoryNameContainer = itemView.findViewById(R.id.categoryNameContainer);
              categoryNameText = itemView.findViewById(R.id.categoryNameText);
          }
      }
  
  }
  </code></pre>
  <p>trendcategory_cell.xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:padding=&quot;5dp&quot;&gt;
  
      &lt;RelativeLayout
          android:id=&quot;@+id/categoryNameContainer&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:background=&quot;@drawable/grey_rounded_bg&quot;&gt;
  
          &lt;TextView
              android:id=&quot;@+id/categoryNameText&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:fontFamily=&quot;@font/gilroyregular&quot;
              android:padding=&quot;15dp&quot;
              android:text=&quot;categoryname&quot;
              android:textColor=&quot;@android:color/black&quot; /&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>TrendingFragment.java:</p>
  <pre><code>public class TrendingFragment extends Fragment implements onCategoryTagClicked {
  
      RecyclerView trendingCategoryRV;
      TextView noPostTV, selectedCategoryText;
  
      FirebaseFirestore db = FirebaseFirestore.getInstance();
      CollectionReference categoryRef;
      String selectedCategory = &quot;Art&quot;;
  
      TrendCategoryTagsAdapter trendCategoryTagsAdapter;
  
      public TrendingFragment() {
          // Required empty public constructor
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.fragment_trending, container, false);
  
          trendPostRV = view.findViewById(R.id.trendPostRV);
          trendingCategoryRV = view.findViewById(R.id.trendingCategoryRV);
          noPostTV = view.findViewById(R.id.noPostTV);
          selectedCategoryText = view.findViewById(R.id.selectedCategoryText);
  
          selectedCategoryText.setText(selectedCategory);
  
          setUpTrendCategoryTagsRV();
          setUpTrendingPostRV();
          // Inflate the layout for this fragment
          return view;
      }
  
      @Override
      public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
          trendCategoryTagsAdapter.startListening();
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          trendCategoryTagsAdapter.stopListening();
      }
  
      private void setUpTrendCategoryTagsRV() {
          categoryRef = db.collection(&quot;Categories&quot;);
  
          Query query = categoryRef.orderBy(&quot;categoryName&quot;, Query.Direction.ASCENDING);
  
          FirestoreRecyclerOptions&lt;CategorySelection&gt; options = new FirestoreRecyclerOptions.Builder&lt;CategorySelection&gt;()
                  .setQuery(query, CategorySelection.class)
                  .build();
  
          trendCategoryTagsAdapter = new TrendCategoryTagsAdapter(options, getContext(), this);
  
          trendingCategoryRV.setNestedScrollingEnabled(false);
  
          final LinearLayoutManager trendingTagsLM = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
          trendingCategoryRV.setLayoutManager(trendingTagsLM);
          trendingCategoryRV.setAdapter(trendCategoryTagsAdapter);
          trendCategoryTagsAdapter.notifyDataSetChanged();
      }
  
      @Override
      public void onTagClick(String categoryName) {
          selectedCategory = categoryName;
      }
  }
  </code></pre>
  <p>Any help will be appreciated. Thanks in advance</p>