title: Fragment in TabLayout only load when user slide Android
body: <p>Hi I'm making an app with A <code>fragment</code> and few child <code>fragment</code> inside it using <code>tablayout</code> and <code>viewpager</code>. The problem is all my child <code>fragment</code> (from <code>Tablayout</code>) always execute (load all the code inside whenever user click the parent <code>fragment</code>. How to get the child <code>fragment</code> only load whenever the the user slide to it (for example I have 2 tab, content in tab 2 only load when user slide to tab 2)</p>
  
  <p>Here is my parent <code>fragment</code> code</p>
  
  <pre><code>public class ManageEventFragment extends Fragment {
  
  
      public ManageEventFragment() {
          // Required empty public constructor
      }
  
  
      TabLayout tabLayout;
      ViewPager viewPager;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View viewFragment = inflater.inflate(R.layout.fragment_manage_event, container, false);
  
          viewPager = (ViewPager) viewFragment.findViewById(R.id.viewPager);
  
          tabLayout = (TabLayout) viewFragment.findViewById(R.id.tabLayout);
  
          viewPager.setAdapter(new CustomAdapter(getChildFragmentManager(), getContext()));
          viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
  
          return viewFragment;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
  
          //        tabLayout.setupWithViewPager(viewPager);
          tabLayout.post(new Runnable() {
              @Override
              public void run() {
                  tabLayout.setupWithViewPager(viewPager);
              }
          });
  
          tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
              @Override
              public void onTabSelected(TabLayout.Tab tab) {
                  viewPager.setCurrentItem(tab.getPosition());
              }
  
              @Override
              public void onTabUnselected(TabLayout.Tab tab) {
                  viewPager.setCurrentItem(tab.getPosition());
              }
  
              @Override
              public void onTabReselected(TabLayout.Tab tab) {
                  viewPager.setCurrentItem(tab.getPosition());
              }
          });
  
          if (ViewCompat.isLaidOut(tabLayout)) {
              tabLayout.setupWithViewPager(viewPager);
          } else {
              tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                  @Override
                  public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                      tabLayout.setupWithViewPager(viewPager);
                      tabLayout.removeOnLayoutChangeListener(this);
                  }
              });
          }
  
          super.onActivityCreated(savedInstanceState);
  
      }
  
  
      //TabLayout and ViewPager class
      private class CustomAdapter extends FragmentPagerAdapter {
  
          private String fragments[] = {"Edit Event", "Create Event"};
  
          public CustomAdapter(FragmentManager fragmentManager, Context context) {
              super(fragmentManager);
          }
  
          @Override
          public Fragment getItem(int position) {
              switch (position) {
                  case 0:
                      return new EditEventFragment();
                  case 1:
                      return new CreateEventFragment();
                  default:
                      return null;
              }
          }
  
          @Override
          public int getCount() {
              return fragments.length;
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              return fragments[position];
          }
  
      }
  
  }
  </code></pre>
  
  <p>And my child fragment code. I want these code only execute whenever the user slide to the child tab. Any help is much appreciate, Thanks</p>
  
  <pre><code>@Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          //Initializing our listEvents list
          listEvents = new ArrayList();
  
          recyclerView.setHasFixedSize(true);
          layoutManager = new LinearLayoutManager(getContext());
          recyclerView.setLayoutManager(layoutManager);
  
          adapter = new EventAdapterEdit(getContext(),listEvents);
          recyclerView.setAdapter(adapter);
  
  
          requestQueue = Volley.newRequestQueue(getContext());
  
          adapter = new EventAdapterEdit(getContext(),listEvents);
          recyclerView.setAdapter(adapter);
  
          getEventDetailRespond(requestQueue);
  </code></pre>