title: Customised TabLayout: notifydatasetchanged revert custom view of tabs back to the default
body: <p>Below is the result I am currently getting.</p>
  
  <p><a href="https://i.stack.imgur.com/6237T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6237T.png" alt="Result"></a></p>
  
  <p>The expected result should be three of them behave as the third tab.</p>
  
  <p>This is how I setup my tab layout.</p>
  
  <p>Reusable method to add fragment to view pager / tab layout.</p>
  
  <pre><code>public void addFragmentToViewPager(Fragment fragment, String title, int imageRes) {
  
      FragmentViewPagerAdapter fragmentViewPagerAdapter =
              (FragmentViewPagerAdapter) mViewPager.getAdapter();
  
      int position = fragmentViewPagerAdapter.addFragment(fragment, title);
  
      fragmentViewPagerAdapter.notifyDataSetChanged();
  
      View tabItemView = LayoutInflater.from(getContext()).inflate(R.layout.tab_item_child, null);
  
      RoundedImageView roundImageView = (RoundedImageView) tabItemView.findViewById(R.id.childitem_riv);
  
      roundImageView.setImageResource(imageRes);
  
      TextView textView = (TextView) tabItemView.findViewById(R.id.childitem_tv_title);
  
      textView.setText(title);
  
      TabLayout.Tab tab = mTabLayout.getTabAt(position);
  
      if (tab != null) {
          Log.d(TAG,tab.getText()+"");
          tab.setCustomView(tabItemView);
      }
  }
  </code></pre>
  
  <p>How I add fragment : </p>
  
  <pre><code>    getView().addFragmentToViewPager(new ReusableMomentFragment(), "One", R.mipmap.ic_launcher);
  
      getView().addFragmentToViewPager(new ReusableMomentFragment(), "Two", R.mipmap.ic_launcher);
  
      getView().addFragmentToViewPager(new ReusableMomentFragment(), "Three", R.mipmap.ic_launcher);
  </code></pre>
  
  <p>And my FragmentViewPagerAdapter : </p>
  
  <pre><code>public class FragmentViewPagerAdapter extends FragmentPagerAdapter {
  
  private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
  private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
  public FragmentViewPagerAdapter(FragmentManager manager) {
      super(manager);
  }
  
  @Override
  public Fragment getItem(int position) {
      return mFragmentList.get(position);
  }
  
  @Override
  public int getCount() {
      return mFragmentList.size();
  }
  
  public int addFragment(Fragment fragment, String title) {
      mFragmentList.add(fragment);
      mFragmentTitleList.add(title);
  
      return getCount() -1 ;
  }
  
  @Override
  public CharSequence getPageTitle(int position) {
      return mFragmentTitleList.get(position);
  }
  }
  </code></pre>
  
  <p>Result for Log.d in addFragmentToViewPager :</p>
  
  <pre><code>04-27 06:49:39.874 14334-14334/com.zxc.zxczxc D/MomentFragment: One
  04-27 06:49:39.877 14334-14334/com.zxc.zxczxc D/MomentFragment: Two
  04-27 06:49:39.878 14334-14334/com.zxc.zxczxc D/MomentFragment: Three
  </code></pre>
  
  <p><strong>Update :</strong></p>
  
  <p>I found out that, after </p>
  
  <blockquote>
    <p>fragmentViewPagerAdapter.notifyDataSetChanged();</p>
  </blockquote>
  
  <p>called, the tab layout will be changed to back to default view. Is there any way to prevent <code>notifyDataSetChanged()</code> not to affect the tab layouts' custom view? Because my tab layout tabs are set on the go (app will fetch the tab content from server and set it up) so my idea is to setup the viewpager first : </p>
  
  <pre><code>FragmentViewPagerAdapter fragmentViewPagerAdapter =
              new FragmentViewPagerAdapter(getActivity().getSupportFragmentManager());
  
      mViewPager.setAdapter(fragmentViewPagerAdapter);
  
      mTabLayout.setupWithViewPager(mViewPager);
  </code></pre>
  
  <p>Then add in the fragment by using :</p>
  
  <pre><code>    public void addFragmentToViewPager(Fragment fragment, String title, int imageRes) {
  
      FragmentViewPagerAdapter fragmentViewPagerAdapter =
              (FragmentViewPagerAdapter) mViewPager.getAdapter();
  
      int position = fragmentViewPagerAdapter.addFragment(fragment, title);
  
      View tabItemView = LayoutInflater.from(getContext()).inflate(R.layout.tab_item_child, null);
  
      RoundedImageView roundImageView = (RoundedImageView) tabItemView.findViewById(R.id.childitem_riv);
  
      roundImageView.setImageResource(imageRes);
  
      TextView textView = (TextView) tabItemView.findViewById(R.id.childitem_tv_title);
  
      textView.setText(title);
  
      TabLayout.Tab tab = mTabLayout.getTabAt(position);
  
      if (tab != null) {
          Log.d(TAG,tab.getText()+"");
          tab.setCustomView(tabItemView);
      }
  }
  </code></pre>
  
  <p>Thanks in advance</p>