diff --git a/progressfragment-native-sample/res/layout/view_header.xml b/progressfragment-native-sample/res/layout/view_header.xml new file mode 100644 index 0000000..b0edb3e --- /dev/null +++ b/progressfragment-native-sample/res/layout/view_header.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/progressfragment-native-sample/src/com/devspark/progressfragment/sample/DefaultDialogProgressFragment.java b/progressfragment-native-sample/src/com/devspark/progressfragment/sample/DefaultDialogProgressFragment.java new file mode 100644 index 0000000..f8025f0 --- /dev/null +++ b/progressfragment-native-sample/src/com/devspark/progressfragment/sample/DefaultDialogProgressFragment.java @@ -0,0 +1,73 @@ +package com.devspark.progressfragment.sample; + +import android.app.DialogFragment; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.devspark.progressfragment.ProgressDialogFragment; + +/** + * Sample implementation of {@link ProgressDialogFragment}. + */ +public class DefaultDialogProgressFragment extends ProgressDialogFragment { + private View mHeaderView; + private View mContentView; + private Handler mHandler; + private Runnable mShowContentRunnable = new Runnable() { + + @Override + public void run() { + setContentShown(true); + } + + }; + + public static DefaultDialogProgressFragment newInstance() { + DefaultDialogProgressFragment fragment = new DefaultDialogProgressFragment(); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + mHeaderView = inflater.inflate(R.layout.view_header, container, false); + mContentView = inflater.inflate(R.layout.view_content, container, false); + return super.onCreateView(inflater, container, savedInstanceState); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + // Setup header view + setHeaderView(mHeaderView); + // Setup content view + setContentView(mContentView); + // Setup text for empty content + setEmptyText(R.string.empty); + obtainData(); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + mHandler.removeCallbacks(mShowContentRunnable); + } + + private void obtainData() { + // Show indeterminate progress + setContentShown(false); + + mHandler = new Handler(); + mHandler.postDelayed(mShowContentRunnable, 3000); + } +} diff --git a/progressfragment-native-sample/src/com/devspark/progressfragment/sample/EmptyContentDialogProgressFragment.java b/progressfragment-native-sample/src/com/devspark/progressfragment/sample/EmptyContentDialogProgressFragment.java new file mode 100644 index 0000000..2caa63e --- /dev/null +++ b/progressfragment-native-sample/src/com/devspark/progressfragment/sample/EmptyContentDialogProgressFragment.java @@ -0,0 +1,71 @@ +package com.devspark.progressfragment.sample; + +import android.app.DialogFragment; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.devspark.progressfragment.ProgressDialogFragment; + +public class EmptyContentDialogProgressFragment extends ProgressDialogFragment { + private View mHeaderView; + private View mContentView; + private Handler mHandler; + private Runnable mShowContentRunnable = new Runnable() { + + @Override + public void run() { + setContentEmpty(true); + setContentShown(true); + } + + }; + + public static EmptyContentDialogProgressFragment newInstance() { + EmptyContentDialogProgressFragment fragment = new EmptyContentDialogProgressFragment(); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + mHeaderView = inflater.inflate(R.layout.view_header, container, false); + mContentView = inflater.inflate(R.layout.view_content, container, false); + return super.onCreateView(inflater, container, savedInstanceState); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + // Setup header view + setHeaderView(mHeaderView); + // Setup content view + setContentView(mContentView); + // Setup text for empty content + setEmptyText(R.string.empty); + obtainData(); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + mHandler.removeCallbacks(mShowContentRunnable); + } + + private void obtainData() { + // Show indeterminate progress + setContentShown(false); + + mHandler = new Handler(); + mHandler.postDelayed(mShowContentRunnable, 3000); + } +} diff --git a/progressfragment-native-sample/src/com/devspark/progressfragment/sample/MainActivity.java b/progressfragment-native-sample/src/com/devspark/progressfragment/sample/MainActivity.java index 80fa7d1..76aac9f 100644 --- a/progressfragment-native-sample/src/com/devspark/progressfragment/sample/MainActivity.java +++ b/progressfragment-native-sample/src/com/devspark/progressfragment/sample/MainActivity.java @@ -28,7 +28,8 @@ */ public class MainActivity extends ListActivity { - private String[] examples = new String[]{"Default", "Empty content", "Custom layout", "List", "Grid"}; + private String[] examples = new String[]{"Default", "Empty content", "Custom layout", "List", "Grid", + "DialogFragment", "Empty content DialogFragment"}; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,6 +58,12 @@ protected void onListItemClick(ListView l, View v, int position, long id) { case 4: intent.putExtra(ProgressActivity.EXTRA_FRAGMENT, ProgressActivity.FRAGMENT_GRID); break; + case 5: + intent.putExtra(ProgressActivity.EXTRA_FRAGMENT, ProgressActivity.DIALOG_FRAGMENT); + break; + case 6: + intent.putExtra(ProgressActivity.EXTRA_FRAGMENT, ProgressActivity.DIALOG_EMPTY_CONTENT); + break; default: break; } diff --git a/progressfragment-native-sample/src/com/devspark/progressfragment/sample/ProgressActivity.java b/progressfragment-native-sample/src/com/devspark/progressfragment/sample/ProgressActivity.java index 08601c8..0a46e6e 100644 --- a/progressfragment-native-sample/src/com/devspark/progressfragment/sample/ProgressActivity.java +++ b/progressfragment-native-sample/src/com/devspark/progressfragment/sample/ProgressActivity.java @@ -34,6 +34,8 @@ public class ProgressActivity extends Activity { public static final int FRAGMENT_CUSTOM_LAYOUT = 2; public static final int FRAGMENT_LIST = 3; public static final int FRAGMENT_GRID = 4; + public static final int DIALOG_FRAGMENT = 5; + public static final int DIALOG_EMPTY_CONTENT = 6; @Override protected void onCreate(Bundle savedInstanceState) { @@ -42,11 +44,21 @@ protected void onCreate(Bundle savedInstanceState) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { new ActionBarHelper().setDisplayHomeAsUpEnabled(true); } + int fragmentId = getIntent().getIntExtra(EXTRA_FRAGMENT, FRAGMENT_DEFAULT); + if (fragmentId == DIALOG_FRAGMENT) { + DefaultDialogProgressFragment df = DefaultDialogProgressFragment.newInstance(); + df.show(getFragmentManager(), "DefaultDialogProgressFragment"); + return; + } else if (fragmentId == DIALOG_EMPTY_CONTENT) { + EmptyContentDialogProgressFragment df = EmptyContentDialogProgressFragment.newInstance(); + df.show(getFragmentManager(), "EmptyContentDialogProgressFragment"); + return; + } + // Check what fragment is shown, replace if needed. Fragment fragment = getFragmentManager().findFragmentById(android.R.id.content); if (fragment == null) { // Make new fragment to show. - int fragmentId = getIntent().getIntExtra(EXTRA_FRAGMENT, FRAGMENT_DEFAULT); switch (fragmentId) { case FRAGMENT_DEFAULT: fragment = DefaultProgressFragment.newInstance(); diff --git a/progressfragment-native/res/layout/fragment_dialog_progress.xml b/progressfragment-native/res/layout/fragment_dialog_progress.xml new file mode 100644 index 0000000..49441d3 --- /dev/null +++ b/progressfragment-native/res/layout/fragment_dialog_progress.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + diff --git a/progressfragment-native/src/com/devspark/progressfragment/ProgressDialogFragment.java b/progressfragment-native/src/com/devspark/progressfragment/ProgressDialogFragment.java new file mode 100644 index 0000000..d9b4e95 --- /dev/null +++ b/progressfragment-native/src/com/devspark/progressfragment/ProgressDialogFragment.java @@ -0,0 +1,349 @@ +package com.devspark.progressfragment; + +import android.app.DialogFragment; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.AnimationUtils; +import android.widget.TextView; + +/** + * The implementation of the fragment to display content. Based on {@link android.app.DialogFragment}. + * If you are waiting for the initial data, you'll can displaying during this time an indeterminate progress indicator. + */ +public class ProgressDialogFragment extends DialogFragment { + + private View mHeaderContainer; + private View mHeaderView; + private View mProgressContainer; + private View mContentContainer; + private View mContentView; + private View mEmptyView; + private boolean mContentShown; + private boolean mIsContentEmpty; + + public ProgressDialogFragment() { + } + + /** + * Provide default implementation to return a simple view. Subclasses + * can override to replace with their own layout. If doing so, the + * returned view hierarchy must have a progress container whose id + * is {@link R.id#progress_container R.id.progress_container}, content container whose id + * is {@link R.id#content_container R.id.content_container} and can optionally + * have a sibling view id {@link android.R.id#empty android.R.id.empty} + * that is to be shown when the content is empty. + *

+ *

If you are overriding this method with your own custom content, + * consider including the standard layout {@link R.layout#fragment_progress} + * in your layout file, so that you continue to retain all of the standard + * behavior of ProgressFragment. In particular, this is currently the only + * way to have the built-in indeterminant progress state be shown. + */ + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_dialog_progress, container, false); + } + + /** + * Attach to view once the view hierarchy has been created. + */ + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + ensureContent(); + } + + /** + * Detach from view. + */ + @Override + public void onDestroyView() { + mContentShown = false; + mIsContentEmpty = false; + mHeaderContainer = mHeaderView = null; + mProgressContainer = mContentContainer = mContentView = mEmptyView = null; + super.onDestroyView(); + } + + /** + * Return header view or null if the header view has not been initialized. + * + * @return header view or null + * @see #setHeaderView(View) + * @see #setHeaderView(int) + */ + public View getHeaderView() { + return mHeaderView; + } + + /** + * Set the header content from a layout resource. + * + * @param layoutResId Resource ID to be inflated. + * @see #setHeaderView(View) + * @see #getHeaderView() + */ + public void setHeaderView(int layoutResId) { + LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); + View headerView = layoutInflater.inflate(layoutResId, null); + setHeaderView(headerView); + } + + /** + * Set the header view to an explicit view. If the header view was installed earlier, + * the content will be replaced with a new view. + * + * @param view The desired header to display. Value can't be null. + * @see #setHeaderView(int) + * @see #getHeaderView() + */ + public void setHeaderView(View view) { + ensureContent(); + if (view == null) { + throw new IllegalArgumentException("Content view can't be null"); + } + if (mHeaderContainer instanceof ViewGroup) { + ViewGroup contentContainer = (ViewGroup) mHeaderContainer; + if (mHeaderView == null) { + contentContainer.addView(view); + } else { + int index = contentContainer.indexOfChild(mHeaderView); + // replace content view + contentContainer.removeView(mHeaderView); + contentContainer.addView(view, index); + } + mHeaderView = view; + } else { + throw new IllegalStateException("Can't be used with a custom content view"); + } + } + + /** + * Return content view or null if the content view has not been initialized. + * + * @return content view or null + * @see #setContentView(View) + * @see #setContentView(int) + */ + public View getContentView() { + return mContentView; + } + + /** + * Set the content content from a layout resource. + * + * @param layoutResId Resource ID to be inflated. + * @see #setContentView(View) + * @see #getContentView() + */ + public void setContentView(int layoutResId) { + LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); + View contentView = layoutInflater.inflate(layoutResId, null); + setContentView(contentView); + } + + /** + * Set the content view to an explicit view. If the content view was installed earlier, + * the content will be replaced with a new view. + * + * @param view The desired content to display. Value can't be null. + * @see #setContentView(int) + * @see #getContentView() + */ + public void setContentView(View view) { + ensureContent(); + if (view == null) { + throw new IllegalArgumentException("Content view can't be null"); + } + if (mContentContainer instanceof ViewGroup) { + ViewGroup contentContainer = (ViewGroup) mContentContainer; + if (mContentView == null) { + contentContainer.addView(view); + } else { + int index = contentContainer.indexOfChild(mContentView); + // replace content view + contentContainer.removeView(mContentView); + contentContainer.addView(view, index); + } + mContentView = view; + } else { + throw new IllegalStateException("Can't be used with a custom content view"); + } + } + + /** + * The default content for a ProgressFragment has a TextView that can be shown when + * the content is empty {@link #setContentEmpty(boolean)}. + * If you would like to have it shown, call this method to supply the text it should use. + * + * @param resId Identification of string from a resources + * @see #setEmptyText(CharSequence) + */ + public void setEmptyText(int resId) { + setEmptyText(getString(resId)); + } + + /** + * The default content for a ProgressFragment has a TextView that can be shown when + * the content is empty {@link #setContentEmpty(boolean)}. + * If you would like to have it shown, call this method to supply the text it should use. + * + * @param text Text for empty view + * @see #setEmptyText(int) + */ + public void setEmptyText(CharSequence text) { + ensureContent(); + if (mEmptyView != null && mEmptyView instanceof TextView) { + ((TextView) mEmptyView).setText(text); + } else { + throw new IllegalStateException("Can't be used with a custom content view"); + } + } + + /** + * Control whether the content is being displayed. You can make it not + * displayed if you are waiting for the initial data to show in it. During + * this time an indeterminant progress indicator will be shown instead. + * + * @param shown If true, the content view is shown; if false, the progress + * indicator. The initial value is true. + * @see #setContentShownNoAnimation(boolean) + */ + public void setContentShown(boolean shown) { + setContentShown(shown, true); + } + + /** + * Like {@link #setContentShown(boolean)}, but no animation is used when + * transitioning from the previous state. + * + * @param shown If true, the content view is shown; if false, the progress + * indicator. The initial value is true. + * @see #setContentShown(boolean) + */ + public void setContentShownNoAnimation(boolean shown) { + setContentShown(shown, false); + } + + /** + * Control whether the content is being displayed. You can make it not + * displayed if you are waiting for the initial data to show in it. During + * this time an indeterminant progress indicator will be shown instead. + * + * @param shown If true, the content view is shown; if false, the progress + * indicator. The initial value is true. + * @param animate If true, an animation will be used to transition to the + * new state. + */ + private void setContentShown(boolean shown, boolean animate) { + ensureContent(); + if (mContentShown == shown) { + return; + } + if (mProgressContainer == null || mContentContainer == null) { + return; + } + mContentShown = shown; + if (shown) { + if (animate) { + mProgressContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out)); + mContentContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in)); + } else { + mProgressContainer.clearAnimation(); + mContentContainer.clearAnimation(); + } + mProgressContainer.setVisibility(View.GONE); + mContentContainer.setVisibility(View.VISIBLE); + } else { + if (animate) { + mProgressContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in)); + mContentContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out)); + } else { + mProgressContainer.clearAnimation(); + mContentContainer.clearAnimation(); + } + mProgressContainer.setVisibility(View.VISIBLE); + mContentContainer.setVisibility(View.GONE); + } + } + + /** + * Returns true if content is empty. The default content is not empty. + * + * @return true if content is null or empty + * @see #setContentEmpty(boolean) + */ + public boolean isContentEmpty() { + return mIsContentEmpty; + } + + /** + * If the content is empty, then set true otherwise false. The default content is not empty. + * You can't call this method if the content view has not been initialized before + * {@link #setContentView(View)} and content view not null. + * + * @param isEmpty true if content is empty else false + * @see #isContentEmpty() + */ + public void setContentEmpty(boolean isEmpty) { + ensureContent(); + if (mContentView == null) { + throw new IllegalStateException("Content view must be initialized before"); + } + if (isEmpty) { + mEmptyView.setVisibility(View.VISIBLE); + mContentView.setVisibility(View.GONE); + } else { + mEmptyView.setVisibility(View.GONE); + mContentView.setVisibility(View.VISIBLE); + } + mIsContentEmpty = isEmpty; + } + + public void hideProgress(boolean isEmpty) { + setContentEmpty(isEmpty); + setContentShown(true); + } + + public void showProgress() { + setContentShown(false); + } + + /** + * Initialization views. + */ + private void ensureContent() { + if (mHeaderContainer != null && mContentContainer != null && mProgressContainer != null) { + return; + } + View root = getView(); + if (root == null) { + throw new IllegalStateException("Content view not yet created"); + } + mHeaderContainer = root.findViewById(R.id.header_container); + if (mHeaderContainer == null) { + throw new RuntimeException("Your content must have a ViewGroup whose id attribute is 'R.id.header_container'"); + } + mProgressContainer = root.findViewById(R.id.progress_container); + if (mProgressContainer == null) { + throw new RuntimeException("Your content must have a ViewGroup whose id attribute is 'R.id.progress_container'"); + } + mContentContainer = root.findViewById(R.id.content_container); + if (mContentContainer == null) { + throw new RuntimeException("Your content must have a ViewGroup whose id attribute is 'R.id.content_container'"); + } + mEmptyView = root.findViewById(android.R.id.empty); + if (mEmptyView != null) { + mEmptyView.setVisibility(View.GONE); + } + mContentShown = true; + // We are starting without a content, so assume we won't + // have our data right away and start with the progress indicator. + if (mContentView == null) { + setContentShown(false, false); + } + } + +} diff --git a/progressfragment-sample/res/layout/view_header.xml b/progressfragment-sample/res/layout/view_header.xml new file mode 100644 index 0000000..b0edb3e --- /dev/null +++ b/progressfragment-sample/res/layout/view_header.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/progressfragment-sample/src/com/devspark/progressfragment/sample/DefaultDialogProgressFragment.java b/progressfragment-sample/src/com/devspark/progressfragment/sample/DefaultDialogProgressFragment.java new file mode 100644 index 0000000..79197b4 --- /dev/null +++ b/progressfragment-sample/src/com/devspark/progressfragment/sample/DefaultDialogProgressFragment.java @@ -0,0 +1,75 @@ +package com.devspark.progressfragment.sample; + +import android.app.DialogFragment; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.devspark.progressfragment.ProgressDialogFragment; + +/** + * Sample implementation of {@link ProgressDialogFragment}. + */ +public class DefaultDialogProgressFragment extends ProgressDialogFragment { + private View mHeaderView; + private View mContentView; + private Handler mHandler; + private Runnable mShowContentRunnable = new Runnable() { + + @Override + public void run() { + setContentShown(true); + } + + }; + + public static DefaultDialogProgressFragment newInstance() { + DefaultDialogProgressFragment fragment = new DefaultDialogProgressFragment(); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + mHeaderView = inflater.inflate(R.layout.view_header, container, false); + mContentView = inflater.inflate(R.layout.view_content, container, false); + return super.onCreateView(inflater, container, savedInstanceState); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + // Setup header view + setHeaderView(mHeaderView); + // Setup content view + setContentView(mContentView); + // Setup text for empty content + setEmptyText(R.string.empty); + obtainData(); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + mHandler.removeCallbacks(mShowContentRunnable); + } + + private void obtainData() { + // Show indeterminate progress + setContentShown(false); + + mHandler = new Handler(); + mHandler.postDelayed(mShowContentRunnable, 3000); + } +} diff --git a/progressfragment-sample/src/com/devspark/progressfragment/sample/EmptyContentDialogProgressFragment.java b/progressfragment-sample/src/com/devspark/progressfragment/sample/EmptyContentDialogProgressFragment.java new file mode 100644 index 0000000..c3c8167 --- /dev/null +++ b/progressfragment-sample/src/com/devspark/progressfragment/sample/EmptyContentDialogProgressFragment.java @@ -0,0 +1,73 @@ +package com.devspark.progressfragment.sample; + +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.support.v4.app.DialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.devspark.progressfragment.ProgressDialogFragment; + +public class EmptyContentDialogProgressFragment extends ProgressDialogFragment { + private View mHeaderView; + private View mContentView; + private Handler mHandler; + private Runnable mShowContentRunnable = new Runnable() { + + @Override + public void run() { + setContentEmpty(true); + setContentShown(true); + } + + }; + + public static EmptyContentDialogProgressFragment newInstance() { + EmptyContentDialogProgressFragment fragment = new EmptyContentDialogProgressFragment(); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + mHeaderView = inflater.inflate(R.layout.view_header, container, false); + mContentView = inflater.inflate(R.layout.view_content, container, false); + return super.onCreateView(inflater, container, savedInstanceState); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + // Setup header view + setHeaderView(mHeaderView); + // Setup content view + setContentView(mContentView); + // Setup text for empty content + setEmptyText(R.string.empty); + obtainData(); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + mHandler.removeCallbacks(mShowContentRunnable); + } + + private void obtainData() { + // Show indeterminate progress + setContentShown(false); + + mHandler = new Handler(); + mHandler.postDelayed(mShowContentRunnable, 3000); + } +} diff --git a/progressfragment-sample/src/com/devspark/progressfragment/sample/MainActivity.java b/progressfragment-sample/src/com/devspark/progressfragment/sample/MainActivity.java index 80fa7d1..76aac9f 100644 --- a/progressfragment-sample/src/com/devspark/progressfragment/sample/MainActivity.java +++ b/progressfragment-sample/src/com/devspark/progressfragment/sample/MainActivity.java @@ -28,7 +28,8 @@ */ public class MainActivity extends ListActivity { - private String[] examples = new String[]{"Default", "Empty content", "Custom layout", "List", "Grid"}; + private String[] examples = new String[]{"Default", "Empty content", "Custom layout", "List", "Grid", + "DialogFragment", "Empty content DialogFragment"}; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,6 +58,12 @@ protected void onListItemClick(ListView l, View v, int position, long id) { case 4: intent.putExtra(ProgressActivity.EXTRA_FRAGMENT, ProgressActivity.FRAGMENT_GRID); break; + case 5: + intent.putExtra(ProgressActivity.EXTRA_FRAGMENT, ProgressActivity.DIALOG_FRAGMENT); + break; + case 6: + intent.putExtra(ProgressActivity.EXTRA_FRAGMENT, ProgressActivity.DIALOG_EMPTY_CONTENT); + break; default: break; } diff --git a/progressfragment-sample/src/com/devspark/progressfragment/sample/ProgressActivity.java b/progressfragment-sample/src/com/devspark/progressfragment/sample/ProgressActivity.java index 05618d5..f8ee788 100644 --- a/progressfragment-sample/src/com/devspark/progressfragment/sample/ProgressActivity.java +++ b/progressfragment-sample/src/com/devspark/progressfragment/sample/ProgressActivity.java @@ -35,6 +35,8 @@ public class ProgressActivity extends FragmentActivity { public static final int FRAGMENT_CUSTOM_LAYOUT = 2; public static final int FRAGMENT_LIST = 3; public static final int FRAGMENT_GRID = 4; + public static final int DIALOG_FRAGMENT = 5; + public static final int DIALOG_EMPTY_CONTENT = 6; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override @@ -44,11 +46,21 @@ protected void onCreate(Bundle savedInstanceState) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { new ActionBarHelper().setDisplayHomeAsUpEnabled(true); } + int fragmentId = getIntent().getIntExtra(EXTRA_FRAGMENT, FRAGMENT_DEFAULT); + if (fragmentId == DIALOG_FRAGMENT) { + DefaultDialogProgressFragment df = DefaultDialogProgressFragment.newInstance(); + df.show(getSupportFragmentManager(), "DefaultDialogProgressFragment"); + return; + } else if (fragmentId == DIALOG_EMPTY_CONTENT) { + EmptyContentDialogProgressFragment df = EmptyContentDialogProgressFragment.newInstance(); + df.show(getSupportFragmentManager(), "EmptyContentDialogProgressFragment"); + return; + } + // Check what fragment is shown, replace if needed. Fragment fragment = getSupportFragmentManager().findFragmentById(android.R.id.content); if (fragment == null) { // Make new fragment to show. - int fragmentId = getIntent().getIntExtra(EXTRA_FRAGMENT, FRAGMENT_DEFAULT); switch (fragmentId) { case FRAGMENT_DEFAULT: fragment = DefaultProgressFragment.newInstance(); @@ -72,6 +84,8 @@ protected void onCreate(Bundle savedInstanceState) { } getSupportFragmentManager().beginTransaction().add(android.R.id.content, fragment).commit(); } + + } @Override diff --git a/progressfragment/res/layout/fragment_dialog_progress.xml b/progressfragment/res/layout/fragment_dialog_progress.xml new file mode 100644 index 0000000..49441d3 --- /dev/null +++ b/progressfragment/res/layout/fragment_dialog_progress.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + diff --git a/progressfragment/src/com/devspark/progressfragment/ProgressDialogFragment.java b/progressfragment/src/com/devspark/progressfragment/ProgressDialogFragment.java new file mode 100644 index 0000000..dbabf11 --- /dev/null +++ b/progressfragment/src/com/devspark/progressfragment/ProgressDialogFragment.java @@ -0,0 +1,349 @@ +package com.devspark.progressfragment; + +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.animation.AnimationUtils; +import android.widget.TextView; + +/** + * The implementation of the fragment to display content. Based on {@link android.support.v4.app.DialogFragment}. + * If you are waiting for the initial data, you'll can displaying during this time an indeterminate progress indicator. + */ +public class ProgressDialogFragment extends DialogFragment { + + private View mHeaderContainer; + private View mHeaderView; + private View mProgressContainer; + private View mContentContainer; + private View mContentView; + private View mEmptyView; + private boolean mContentShown; + private boolean mIsContentEmpty; + + public ProgressDialogFragment() { + } + + /** + * Provide default implementation to return a simple view. Subclasses + * can override to replace with their own layout. If doing so, the + * returned view hierarchy must have a progress container whose id + * is {@link com.devspark.progressfragment.R.id#progress_container R.id.progress_container}, content container whose id + * is {@link com.devspark.progressfragment.R.id#content_container R.id.content_container} and can optionally + * have a sibling view id {@link android.R.id#empty android.R.id.empty} + * that is to be shown when the content is empty. + *

+ *

If you are overriding this method with your own custom content, + * consider including the standard layout {@link com.devspark.progressfragment.R.layout#fragment_progress} + * in your layout file, so that you continue to retain all of the standard + * behavior of ProgressFragment. In particular, this is currently the only + * way to have the built-in indeterminant progress state be shown. + */ + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_dialog_progress, container, false); + } + + /** + * Attach to view once the view hierarchy has been created. + */ + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + ensureContent(); + } + + /** + * Detach from view. + */ + @Override + public void onDestroyView() { + mContentShown = false; + mIsContentEmpty = false; + mHeaderContainer = mHeaderView = null; + mProgressContainer = mContentContainer = mContentView = mEmptyView = null; + super.onDestroyView(); + } + + /** + * Return header view or null if the header view has not been initialized. + * + * @return header view or null + * @see #setHeaderView(android.view.View) + * @see #setHeaderView(int) + */ + public View getHeaderView() { + return mHeaderView; + } + + /** + * Set the header content from a layout resource. + * + * @param layoutResId Resource ID to be inflated. + * @see #setHeaderView(android.view.View) + * @see #getHeaderView() + */ + public void setHeaderView(int layoutResId) { + LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); + View headerView = layoutInflater.inflate(layoutResId, null); + setHeaderView(headerView); + } + + /** + * Set the header view to an explicit view. If the header view was installed earlier, + * the content will be replaced with a new view. + * + * @param view The desired header to display. Value can't be null. + * @see #setHeaderView(int) + * @see #getHeaderView() + */ + public void setHeaderView(View view) { + ensureContent(); + if (view == null) { + throw new IllegalArgumentException("Content view can't be null"); + } + if (mHeaderContainer instanceof ViewGroup) { + ViewGroup contentContainer = (ViewGroup) mHeaderContainer; + if (mHeaderView == null) { + contentContainer.addView(view); + } else { + int index = contentContainer.indexOfChild(mHeaderView); + // replace content view + contentContainer.removeView(mHeaderView); + contentContainer.addView(view, index); + } + mHeaderView = view; + } else { + throw new IllegalStateException("Can't be used with a custom content view"); + } + } + + /** + * Return content view or null if the content view has not been initialized. + * + * @return content view or null + * @see #setContentView(android.view.View) + * @see #setContentView(int) + */ + public View getContentView() { + return mContentView; + } + + /** + * Set the content content from a layout resource. + * + * @param layoutResId Resource ID to be inflated. + * @see #setContentView(android.view.View) + * @see #getContentView() + */ + public void setContentView(int layoutResId) { + LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); + View contentView = layoutInflater.inflate(layoutResId, null); + setContentView(contentView); + } + + /** + * Set the content view to an explicit view. If the content view was installed earlier, + * the content will be replaced with a new view. + * + * @param view The desired content to display. Value can't be null. + * @see #setContentView(int) + * @see #getContentView() + */ + public void setContentView(View view) { + ensureContent(); + if (view == null) { + throw new IllegalArgumentException("Content view can't be null"); + } + if (mContentContainer instanceof ViewGroup) { + ViewGroup contentContainer = (ViewGroup) mContentContainer; + if (mContentView == null) { + contentContainer.addView(view); + } else { + int index = contentContainer.indexOfChild(mContentView); + // replace content view + contentContainer.removeView(mContentView); + contentContainer.addView(view, index); + } + mContentView = view; + } else { + throw new IllegalStateException("Can't be used with a custom content view"); + } + } + + /** + * The default content for a ProgressFragment has a TextView that can be shown when + * the content is empty {@link #setContentEmpty(boolean)}. + * If you would like to have it shown, call this method to supply the text it should use. + * + * @param resId Identification of string from a resources + * @see #setEmptyText(CharSequence) + */ + public void setEmptyText(int resId) { + setEmptyText(getString(resId)); + } + + /** + * The default content for a ProgressFragment has a TextView that can be shown when + * the content is empty {@link #setContentEmpty(boolean)}. + * If you would like to have it shown, call this method to supply the text it should use. + * + * @param text Text for empty view + * @see #setEmptyText(int) + */ + public void setEmptyText(CharSequence text) { + ensureContent(); + if (mEmptyView != null && mEmptyView instanceof TextView) { + ((TextView) mEmptyView).setText(text); + } else { + throw new IllegalStateException("Can't be used with a custom content view"); + } + } + + /** + * Control whether the content is being displayed. You can make it not + * displayed if you are waiting for the initial data to show in it. During + * this time an indeterminant progress indicator will be shown instead. + * + * @param shown If true, the content view is shown; if false, the progress + * indicator. The initial value is true. + * @see #setContentShownNoAnimation(boolean) + */ + public void setContentShown(boolean shown) { + setContentShown(shown, true); + } + + /** + * Like {@link #setContentShown(boolean)}, but no animation is used when + * transitioning from the previous state. + * + * @param shown If true, the content view is shown; if false, the progress + * indicator. The initial value is true. + * @see #setContentShown(boolean) + */ + public void setContentShownNoAnimation(boolean shown) { + setContentShown(shown, false); + } + + /** + * Control whether the content is being displayed. You can make it not + * displayed if you are waiting for the initial data to show in it. During + * this time an indeterminant progress indicator will be shown instead. + * + * @param shown If true, the content view is shown; if false, the progress + * indicator. The initial value is true. + * @param animate If true, an animation will be used to transition to the + * new state. + */ + private void setContentShown(boolean shown, boolean animate) { + ensureContent(); + if (mContentShown == shown) { + return; + } + if (mProgressContainer == null || mContentContainer == null) { + return; + } + mContentShown = shown; + if (shown) { + if (animate) { + mProgressContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out)); + mContentContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in)); + } else { + mProgressContainer.clearAnimation(); + mContentContainer.clearAnimation(); + } + mProgressContainer.setVisibility(View.GONE); + mContentContainer.setVisibility(View.VISIBLE); + } else { + if (animate) { + mProgressContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in)); + mContentContainer.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out)); + } else { + mProgressContainer.clearAnimation(); + mContentContainer.clearAnimation(); + } + mProgressContainer.setVisibility(View.VISIBLE); + mContentContainer.setVisibility(View.GONE); + } + } + + /** + * Returns true if content is empty. The default content is not empty. + * + * @return true if content is null or empty + * @see #setContentEmpty(boolean) + */ + public boolean isContentEmpty() { + return mIsContentEmpty; + } + + /** + * If the content is empty, then set true otherwise false. The default content is not empty. + * You can't call this method if the content view has not been initialized before + * {@link #setContentView(android.view.View)} and content view not null. + * + * @param isEmpty true if content is empty else false + * @see #isContentEmpty() + */ + public void setContentEmpty(boolean isEmpty) { + ensureContent(); + if (mContentView == null) { + throw new IllegalStateException("Content view must be initialized before"); + } + if (isEmpty) { + mEmptyView.setVisibility(View.VISIBLE); + mContentView.setVisibility(View.GONE); + } else { + mEmptyView.setVisibility(View.GONE); + mContentView.setVisibility(View.VISIBLE); + } + mIsContentEmpty = isEmpty; + } + + public void hideProgress(boolean isEmpty) { + setContentEmpty(isEmpty); + setContentShown(true); + } + + public void showProgress() { + setContentShown(false); + } + + /** + * Initialization views. + */ + private void ensureContent() { + if (mHeaderContainer != null && mContentContainer != null && mProgressContainer != null) { + return; + } + View root = getView(); + if (root == null) { + throw new IllegalStateException("Content view not yet created"); + } + mHeaderContainer = root.findViewById(R.id.header_container); + if (mHeaderContainer == null) { + throw new RuntimeException("Your content must have a ViewGroup whose id attribute is 'R.id.header_container'"); + } + mProgressContainer = root.findViewById(R.id.progress_container); + if (mProgressContainer == null) { + throw new RuntimeException("Your content must have a ViewGroup whose id attribute is 'R.id.progress_container'"); + } + mContentContainer = root.findViewById(R.id.content_container); + if (mContentContainer == null) { + throw new RuntimeException("Your content must have a ViewGroup whose id attribute is 'R.id.content_container'"); + } + mEmptyView = root.findViewById(android.R.id.empty); + if (mEmptyView != null) { + mEmptyView.setVisibility(View.GONE); + } + mContentShown = true; + // We are starting without a content, so assume we won't + // have our data right away and start with the progress indicator. + if (mContentView == null) { + setContentShown(false, false); + } + } + +}