title: Refresh Fragment View on Tab Change
body: <p>Hello I'm implementing a dynamic <code>TabLayout</code> using a single fragment, i have manage to create the tabs so far but there is a problem using a single fragment. When im changing the tabs the fragment view is not updating  repeatedly, although im updating the data in the fragment's <code>onCreateView</code> method based on the tab selection. I have a <code>recyclerview</code> in the that fragment which should change according to the tab selection. SO can anyone help me with it or do anyone have a better way of approach? </p>
  
  <p>TabView Activity</p>
  
  <pre><code>public class SubCategory extends AppCompatActivity {
  
  private Toolbar toolbar;
  private TabLayout tabLayout;
  private ViewPager viewPager;
  Activity a;
  String url;
  List&lt;CategoryEncapsulation&gt; categories = new ArrayList&lt;&gt;();
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_sub_category);
      a=this;
      toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      viewPager = (ViewPager) findViewById(R.id.viewpager);
      tabLayout = (TabLayout) findViewById(R.id.tabs);
      categories=((MyshinzGlobal)getApplicationContext()).getCategoryList();
      setupViewPager(viewPager);
      ((MyshinzGlobal)getApplicationContext()).setCategory_id(categories.get(0).getCategory_id());
  }
  
  private void setupViewPager(ViewPager viewPager) {
      ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
      try {
  
          for (int i = 0; i &lt; categories.size(); i++) {
              try {
                  adapter.addFragment(new ListFragment(), categories.get(i).getCategory_name());
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }catch (Exception e) {
          e.printStackTrace();
      }
      viewPager.setAdapter(adapter);
      tabLayout.setupWithViewPager(viewPager);
      viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
          }
  
          @Override
          public void onPageSelected(int position) {
          ((MyshinzGlobal)getApplicationContext()).setCategory_id(categories.get(position).getCategory_id());
          }
  
          @Override
          public void onPageScrollStateChanged(int state) {
  
          }
      });
  
  }
  
  
  class ViewPagerAdapter 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 ViewPagerAdapter(FragmentManager manager) {
          super(manager);
      }
  
  
      @Override
      public Fragment getItem(int position) {
          return mFragmentList.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragmentList.size();
      }
  
      public void addFragment(Fragment fragment, String title) {
          mFragmentList.add(fragment);
          mFragmentTitleList.add(title);
      }
  
      @Override
      public int getItemPosition(Object object) {
          return super.getItemPosition(object);
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return mFragmentTitleList.get(position);
      }
  }
  }
  </code></pre>
  
  <p>Fragment class</p>
  
  <pre><code>public class ListFragment extends Fragment{
  
  String category_id;
  Activity a;
  RecyclerView rv;
  SubCategoryAdapter sca;
  List&lt;ParentListItem&gt; parentListItems;
  List&lt;CategoryEncapsulation&gt; categories = new ArrayList&lt;&gt;();
  
  
  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view =inflater.inflate(R.layout.sub_category_fragment, container, false);
      a=getActivity();
      categories=((MyshinzGlobal)a.getApplicationContext()).getCategoryList();
      category_id=((MyshinzGlobal)a.getApplicationContext()).getCategory_id();
      parentListItems= new ArrayList&lt;&gt;();
      Toast.makeText(getActivity(), String.valueOf(category_id), Toast.LENGTH_SHORT).show();
      rv= (RecyclerView) view.findViewById(R.id.recycler_view);
  
      for (int i=0; i&lt;categories.size();i++){
          if(categories.get(i).getCategory_id().equals(category_id)){
              parentListItems=categories.get(i).getParentListItems();
          }
      }
      sca=new SubCategoryAdapter(a,parentListItems);
      rv.setAdapter(sca);
      rv.setLayoutManager(new LinearLayoutManager(a));
      //new GetAllSubCategoryTask(a,url,"1001").execute();
      return view;
  }
  
  
   }
  </code></pre>