Skip to content

Instantly share code, notes, and snippets.

@juliomarcos
Last active August 29, 2015 14:05
Show Gist options
  • Save juliomarcos/2c31ad9b638e1d63566f to your computer and use it in GitHub Desktop.
Save juliomarcos/2c31ad9b638e1d63566f to your computer and use it in GitHub Desktop.
PreLoadingFragment
package /* YOUR PACKAGE HERE */;
package cc.edinteractive.visitavirtual.fragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import cc.edinteractive.visitavirtual.R;
/**
* <p>Extend this class to have a ProgressBar initially attached to your fragment.</p>
* <p>Do your networking or database stuff using {@link android.support.v4.app.Fragment#onStart()} or {@link android.support.v4.app.Fragment#onResume()}.
* After you're done with them, call {@link #signalLoaded(Object)} passing the object acquired through the long request.</p>
* @author Julio Rodrigues
*/
public abstract class PreLoadingFragment<T> extends Fragment {
private LayoutInflater inflater;
private ViewGroup container;
private Bundle savedInstanceState;
private Object internetData;
protected Activity activity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
this.inflater = inflater;
this.container = container;
this.savedInstanceState = savedInstanceState;
return inflater.inflate(R.layout.spaced_progress_bar, container, false).findViewById(R.id.progress_bar);
}
public abstract View onLoaded(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState, T internetData);
/**
* Call this method when the long request is done
* @param internetData
*/
public void signalLoaded(T internetData) {
this.internetData = internetData;
container = removeLoader();
container.addView(onLoaded(inflater, container, savedInstanceState, internetData));
}
/**
* Removes the Progress Bar. This should only be called after onCreateView()
* @return ViewGroup returns the fragment container view
*/
private ViewGroup removeLoader() {
ViewGroup root = (ViewGroup) getView();
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
root.setLayoutParams(lp);
root.removeView(root.findViewById(R.id.progress_bar));
return root;
}
}
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleLarge"
android:layout_gravity="center"
android:paddingTop="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment