Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Last active November 1, 2017 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afiqiqmal/55b7052a85211a11546508d9c2cfc873 to your computer and use it in GitHub Desktop.
Save afiqiqmal/55b7052a85211a11546508d9c2cfc873 to your computer and use it in GitHub Desktop.
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.
public interface OnFragmentFinishLoad {
public void onFinish(String tag,boolean state);
}
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);
}
}
}
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment