title: Unable to add tab badge from a fragment on data changed
body: <p>In my app, I have a tab layout with 2 tabs. I am using <code>FragmentPagerAdapter</code> to create fragments for each tab. In one of these fragments, I have a <code>RecyclerView</code> that has a <code>RecyclerView.Adapter</code> attached which takes care of the data binding to the list items in the <code>RecyclerView</code>. When the user selects an item from this list, I am adding the corresponding item to another list. I want to show a badge on the second tab when an item from the first list is selected. I am following the guidelines in <a href="https://material.io/develop/android/components/tabs" rel="nofollow noreferrer">https://material.io/develop/android/components/tabs</a> but I am not able to add the badge to the second tab.</p>
  <p>How can I get a reference to the second tab in my RecyclerView adapter class (because this is where the handler for item selection is present) so that I can change the tab badge? Is there a better place where I can change the tab badge?</p>
  <p>Following is code for the RecyclerView adapter:</p>
  <pre><code>class RecyclerAdapter(val context: Context): RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;() {
  
      inner class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
          var itemTitle: TextView
          var checkBox: CheckBox
          init {
              itemTitle = itemView.findViewById(R.id.li_item_title)
              checkBox = itemView.findViewById(R.id.li_chk_add)
  
              checkBox.setOnClickListener {
                  SharedData.allItems[adapterPosition].isSelected = checkBox.isChecked
                  if(checkBox.isChecked) {
                      SharedData.selectedItems.add(SharedData.allItems[adapterPosition])
                  } else {
                      SharedData.removeItem(itemTitle.text.toString())
                  }
              }
          }
      }
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
          val v = LayoutInflater.from(context).inflate(R.layout.list_item_view, parent, false)
          return MyViewHolder(v)
      }
  
      override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
          holder.itemView.findViewById&lt;TextView&gt;(R.id.li_item_title).text =
              SharedData.allItems[position].title
          holder.itemView.findViewById&lt;CheckBox&gt;(R.id.li_chk_add).isChecked =
              SharedData.allItems[position].isSelected
      }
  
      override fun getItemCount(): Int {
          return SharedData.allItems.size
      }
  }
  </code></pre>
  <p>Following is the code for the fragment that contains the RecyclerView:</p>
  <pre><code>class FragmentItems: Fragment() {
  
      override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          // recycler view specific
          val view = inflater.inflate(R.layout.fragment_items, null)
          val layoutManager = LinearLayoutManager(context)
          val recycler_view = view.findViewById&lt;RecyclerView&gt;(R.id.items_list)
          recycler_view.layoutManager = layoutManager
          val recyclerAdapter = RecyclerAdapter(context!!)
          recycler_view.adapter = recyclerAdapter
          return view
      }
  }
  </code></pre>
  <p>Here's the code for the layout of the fragment:</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;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:orientation=&quot;horizontal&quot;
      android:layout_marginStart=&quot;10dp&quot;
      android:layout_marginEnd=&quot;10dp&quot;
      android:layout_marginTop=&quot;5dp&quot;
      android:layout_marginBottom=&quot;5dp&quot;&gt;
      &lt;LinearLayout
          android:id=&quot;@+id/text_layout&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:orientation=&quot;vertical&quot;
          android:padding=&quot;3dp&quot;
          android:clickable=&quot;true&quot;&gt;
          &lt;TextView
              android:id=&quot;@+id/li_item_title&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:textSize=&quot;22sp&quot;/&gt;
      &lt;/LinearLayout&gt;
      &lt;CheckBox
          android:id=&quot;@+id/li_chk_add&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:focusable=&quot;true&quot;
          android:layout_alignParentEnd=&quot;true&quot;
          android:layout_centerVertical=&quot;true&quot;/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>Following is the code from MainActivity where the tabs are added:</p>
  <pre><code>val tabLayout = findViewById&lt;TabLayout&gt;(R.id.tab_layout)
  val viewPager = findViewById&lt;ViewPager&gt;(R.id.tab_view_pager)
  val viewPagerAdapter = MyViewAdapter(supportFragmentManager, tabLayout.tabCount)
  viewPager.adapter = viewPagerAdapter
  tabLayout.setupWithViewPager(viewPager)
  </code></pre>
  <p>Here's the code for the <code>FragmentPagerAdapter</code>:</p>
  <pre><code>class MyViewAdapter(fragmentMangaer:FragmentManager, tabCount: Int) : FragmentPagerAdapter(fragmentMangaer, tabCount) {
  
      lateinit var listFragment:Fragment
  
      override fun getItem(position: Int): Fragment {
          println(&quot;in getItem&quot;)
          var fragment:Fragment = Fragment()
          when(position) {
              0 -&gt; {
                  fragment = FragmentItems()
              }
              1 -&gt; {
                  fragment =  FragmentList()
                  listFragment = fragment
              }
          }
          return fragment
      }
  
      override fun getCount(): Int {
          return 2
      }
  
      override fun getPageTitle(position: Int): CharSequence? {
          var title = &quot;None&quot;
          when(position) {
              0 -&gt; {title = &quot;Items&quot;}
              1 -&gt; {title = &quot;List&quot;}
          }
          return title
      }
  
      fun getShoppingListFragment(): Fragment {
          return listFragment
      }
  }
  </code></pre>