Wednesday, October 12, 2016

Android Viewpager Wait Fragment Finish Load

After several attempt on how to wait all the fragment in view pager is loaded, i came out a solution where i create interface class to communicate between activity and the fragment..
  1. Create Interface Class
  2. Implement it on Activity class
  3. Call it from each fragment at the off each process

Change Snippet Background Color



Create Interface as bridge between fragment and activity

public interface OnFragmentFinishLoad {
    public void onFinish(String tag,boolean state);
}
 

Now Create Activity Class contain Viewpager.. (Code not complete)

public class PizzaActivity extends BaseActivity implements OnFragmentFinishLoad {

   @BindView(R.id.cover_rl)
 
   protected RelativeLayout cover_rl;

   int countLoad = 0;

   @Override
 public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_carte_tab_layout);
      cover_rl.setVisibility(View.VISIBLE);


      setupViewPager(viewPager);

      mTabLayout.setupWithViewPager(viewPager);

      setupTabLayout(mTabLayout);

      ……
   }

   @Override
 public void onFinish(String tag, boolean state) {

      if (state)
 
         countLoad++;

      
      //cat.size() is the number of fragment in view pager.
      //each time fragment trigger this function, there will return true and countLoad will increase
      //if number of true == cat.size() the loading will disappear
  

      if (countLoad == cat.size() - 1) {

         cover_rl.setVisibility(View.GONE);

      }

   }
}
 
Then, Create Fragment class, 

public class PizzaFragment extends BaseFragment {

   @Override
 
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      return inflater.inflate(R.layout.default_fragment, container, false);

   }

   @Override
 
   public void onViewCreated(View view, Bundle savedInstanceState) {

       super.onViewCreated(view, savedInstanceState);

       ...
       ...

       
       //run task
       new AsyncDataTaskPizza().execute();
   }

   private class AsyncDataTaskPizza extends AsyncTask < Void, Void, Void > {
      @Override
 
      protected void onPreExecute() {

         super.onPreExecute();

      }

      @Override
      protected Void doInBackground(Void... params) {
           //HTTP Call or else
      }

      @Override
 
      protected void onPostExecute(List < PizzaDetail > result) {

          // call this function after finish load everything
          ((OnFragmentFinishLoad) getActivity()).onFinish(null, true);

      }
   }

}
 
Create other Fragment class, do like the same or something else but last process must end with ((OnFragmentFinishLoad) getActivity()).onFinish(null, true);


This is how i manage and i found that more easier to listen when all fragment in ViewPager is finish loading.  :D

1 comment:

  1. Wonderfully explained. Test Automation Framework
    is an automated software testing tool that makes it easy for you to create, deploy, and manage your automated tests. Eiliana.com is a freelancing platform that provides a safe environment where you can get quality services from reliable professionals at affordable prices. Sign up today.

    ReplyDelete