如何在android中的单例类中创建进度条

时间:2016-03-29 04:11:13

标签: android

我有一个单独布局的进度条,我想在singletonclass中进行progressbar实现,并在所有frament中使用singleton类的方法。我该怎么做。

这是我的progressbar.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="65dp"
            android:background="#43A6F3"
            android:orientation="vertical">

<TextView
    android:id="@+id/progress_text"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_above="@+id/progress_bar"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:background="@android:color/transparent"
    android:gravity="center_vertical"
    android:text=""
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:visibility="gone" />

<ProgressBar
    android:id="@+id/progress_bar"
    style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="center"
    android:background="#FFFF"
    android:maxHeight="20dp"
    android:minHeight="20dp" />

这是我的单身人士课程

public class CShowProgress {
public static CShowProgress s_m_oCShowProgress;
public static Context m_Context;
public Dialog m_Dialog;
public ProgressBar m_ProgressBar;

public CShowProgress(Context m_Context) {
    this.m_Context = m_Context;

}

public static CShowProgress getInstance() {
    if (s_m_oCShowProgress == null) {
        s_m_oCShowProgress = new CShowProgress(m_Context);
    }
    return s_m_oCShowProgress;
}

public void showProgress(String message) {
    m_Dialog = new Dialog(m_Context);
    m_Dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    m_Dialog.setContentView(R.layout.progress_bar);
    m_ProgressBar = (ProgressBar) m_Dialog.findViewById(R.id.progress_bar);
    TextView progressText = (TextView) m_Dialog.findViewById(R.id.progress_text);
    progressText.setText("" + message);
    progressText.setVisibility(View.VISIBLE);
    m_ProgressBar.setVisibility(View.VISIBLE);
    m_ProgressBar.setIndeterminate(true);
    m_Dialog.setCancelable(false);
    m_Dialog.setCanceledOnTouchOutside(false);
    m_Dialog.show();
}

这是我的片段/活动,我必须使用showProgress()

protected void onPreExecute() {
        super.onPreExecute();

        CShowProgress.getInstance.showProgress("Please wait while Loading Deals...");
    }

2 个答案:

答案 0 :(得分:4)

public class CShowProgress {
public static CShowProgress s_m_oCShowProgress;

public Dialog m_Dialog;
private ProgressBar m_ProgressBar;
private Context context;

private CShowProgress(Context context) {
    this.context = context;

}

public static CShowProgress getInstance() {
    if (s_m_oCShowProgress == null) {
        s_m_oCShowProgress = new CShowProgress(m_Context);
    }
    return s_m_oCShowProgress;
}

public void showProgress(Context m_Context,String message) {
    m_Dialog = new Dialog(m_Context);
    m_Dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    m_Dialog.setContentView(R.layout.progress_bar);
    m_ProgressBar = (ProgressBar) m_Dialog.findViewById(R.id.progress_bar);
    TextView progressText = (TextView) m_Dialog.findViewById(R.id.progress_text);
    progressText.setText("" + message);
    progressText.setVisibility(View.VISIBLE);
    m_ProgressBar.setVisibility(View.VISIBLE);
    m_ProgressBar.setIndeterminate(true);
    m_Dialog.setCancelable(false);
    m_Dialog.setCanceledOnTouchOutside(false);
    m_Dialog.show();
}
use it like this in fragments

CShowProgress progressbar = CShowProgress.getInstance();

progressbar.showProgress(getActivity(),"Your message")

答案 1 :(得分:1)

我的单身ProgressDialog

public class ProgressDialog {
    private Dialog dialog;
    private static ProgressDialog mInstance;

    public static synchronized ProgressDialog getInstance() {
        if (mInstance == null) {
            mInstance = new ProgressDialog();
        }
        return mInstance;
    }

    public void show(Context context) {
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.layout_progress_dialog);
        dialog.setCancelable(false);
        dialog.show();
    }

    public void dismiss() {
       if (dialog != null && dialog.isShowing()) {
          dialog.dismiss();
       }
    }
}