瀏覽代碼

Added the ability to display min/max progress in determinate progress dialog. Resolved #328.

Aidan Follestad 10 年之前
父節點
當前提交
8062d39b1c

+ 27 - 0
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -72,6 +72,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
     protected FrameLayout customViewFrame;
     protected ProgressBar mProgress;
     protected TextView mProgressLabel;
+    protected TextView mProgressMinMax;
     protected TextView content;
 
     protected View positiveButton;
@@ -149,6 +150,16 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
                 mProgress.setProgress(0);
                 mProgress.setMax(mBuilder.mProgressMax);
                 mProgressLabel = (TextView) mBuilder.customView.findViewById(R.id.label);
+                mProgressMinMax = (TextView) mBuilder.customView.findViewById(R.id.minMax);
+                if (mBuilder.mShowMinMax) {
+                    mProgressMinMax.setVisibility(View.VISIBLE);
+                    mProgressMinMax.setText("0/" + mBuilder.mProgressMax);
+                    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mProgress.getLayoutParams();
+                    lp.leftMargin = 0;
+                    lp.rightMargin = 0;
+                } else {
+                    mProgressMinMax.setVisibility(View.GONE);
+                }
                 mProgressLabel.setText("0%");
             }
             int bottomPadding = (int) getContext().getResources().getDimension(R.dimen.md_dialog_frame_margin);
@@ -951,6 +962,7 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
         protected int backgroundColor;
         protected int itemColor;
         protected boolean mIndeterminateProgress;
+        protected boolean mShowMinMax;
         protected int mProgress = -2;
         protected int mProgressMax = 0;
 
@@ -1362,6 +1374,19 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
             return this;
         }
 
+        /**
+         * Makes this dialog a progress dialog.
+         *
+         * @param indeterminate If true, an infinite circular spinner is shown. If false, a horizontal progress bar is shown that is incremented or set via the built MaterialDialog instance.
+         * @param max           When indeterminate is false, the max value the horizontal progress bar can get to.
+         * @param showMinMax    For determinate dialogs, the min and max will be displayed to the left (start) of the progress bar, e.g. 50/100.
+         * @return An instance of the Builder so calls can be chained.
+         */
+        public Builder progress(boolean indeterminate, int max, boolean showMinMax) {
+            this.mShowMinMax = showMinMax;
+            return progress(indeterminate, max);
+        }
+
         public Builder positiveColor(int color) {
             this.positiveColor = color;
             return this;
@@ -1763,6 +1788,8 @@ public class MaterialDialog extends DialogBase implements View.OnClickListener,
         mProgress.setProgress(progress);
         int percentage = (int) (((float) getCurrentProgress() / (float) getMaxProgress()) * 100f);
         mProgressLabel.setText(percentage + "%");
+        if (mProgressMinMax != null)
+            mProgressMinMax.setText(getCurrentProgress() + "/" + getMaxProgress());
     }
 
     public final void setMaxProgress(int max) {

+ 18 - 0
library/src/main/res/layout/md_progress_dialog.xml

@@ -21,6 +21,22 @@
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
 
+        <TextView
+            android:id="@+id/minMax"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            tools:text="50/100"
+            android:textSize="14sp"
+            android:layout_marginLeft="12dp"
+            android:layout_marginStart="12dp"
+            android:layout_marginRight="12dp"
+            android:layout_marginEnd="12dp"
+            android:gravity="start"
+            android:textAlignment="viewStart"
+            android:layout_alignParentLeft="true"
+            android:layout_alignParentStart="true"
+            android:layout_centerVertical="true" />
+
         <ProgressBar
             android:id="@android:id/progress"
             android:layout_width="match_parent"
@@ -29,6 +45,8 @@
             android:layout_marginLeft="@dimen/md_dialog_frame_margin"
             android:layout_marginStart="@dimen/md_dialog_frame_margin"
             android:layout_marginBottom="@dimen/md_dialog_frame_margin"
+            android:layout_toRightOf="@+id/minMax"
+            android:layout_toEndOf="@+id/minMax"
             android:layout_toLeftOf="@+id/label"
             android:layout_toStartOf="@+id/label"
             android:layout_centerVertical="true" />

+ 1 - 1
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -503,7 +503,7 @@ public class MainActivity extends ActionBarActivity implements FolderSelectorDia
                     .title(R.string.progress_dialog)
                     .content(R.string.please_wait)
                     .contentGravity(GravityEnum.CENTER)
-                    .progress(false, 150)
+                    .progress(false, 150, true)
                     .showListener(new DialogInterface.OnShowListener() {
                         @Override
                         public void onShow(DialogInterface dialogInterface) {