现在的位置: 首页 > 综合 > 正文

通过Activity的ViewGroup添加ProgressBar

2018年09月02日 ⁄ 综合 ⁄ 共 4677字 ⁄ 字号 评论关闭

ProgressBar是一个常见的组件,我们可以通过多种方式来实现一个progressBar,有一种方式是通过一个Activity的的window来放置一个bar,因为android中大家都知道的关联关系是:Activity->Window->View[ViewGroup]  ,那我们通过当前Activity的window来加载一个progressBar,当我们把这个Activity作为一个基类的时候,那么它的子类和我们附加在上面的Fragment就可以轻松的展示一个progressbar了[我们把这个自定义的progressbar做一个单例即可]

具体的实现流程如下:

1:需要展示的progressbar布局

      一个linearLayout里面包含一个要提示的TextView和一个进度条

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00000000"
    android:gravity="center" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/sdk_semitransparent"
        android:gravity="center"
        android:padding="15dp" >

        <ProgressBar
            style="@style/sdk_load_progress_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateDrawable="@drawable/sdk_load_progressbar" />

        <TextView
            android:id="@+id/progress_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="@string/sdk_loading" >
        </TextView>
    </LinearLayout>

</LinearLayout>

2:实现自定义progressbar

     里面的注释还是比较清楚的,就是通过Activity获取到一个Window,然后添加进来一个我们需要展示的布局

public class ProgressView {
    //要展示的progressbar布局
    private View mDialogView;
    //从当前Activity获取的ViewGroup 【mFLayout】
    private ViewGroup mFragmentView;
    //新建一个layout,放置我们的progressbar,主要作用是设置一些现实属性
    private LinearLayout mFLayout;
   
    //单例对象
    private static ProgressView mInstance;
    //需要提示的文字View
    private TextView mProgressText;
    //设置是否可取消
    private boolean mCancelable = true;

    private ProgressView() {

    }

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

    public void showProgressView(Activity act, int loadingTextId,
            boolean cancelable) {
        showProgressView(act, ResUtil.getString(loadingTextId), cancelable);
    }

    public void showProgressView(Activity act, int loadingTextId) {
        showProgressView(act, ResUtil.getString(loadingTextId), true);
    }

    public void showProgressView(Activity act, String tipStr) {
        showProgressView(act, tipStr, true);
    }

    public void showDilaogProgressView(Activity act, String tipStr) {
        dialogProgressView(act, tipStr, true);
    }

    /**
     * 
     * 〈一句话功能简述〉<br>
     * 〈功能详细描述〉
     * 
     * @param act content
     * @param loading_text progress显示的String
     */
    public void showProgressView(Activity act, String loading_text,
            boolean cancelable) {
        mDialogView = act.getLayoutInflater().inflate(
                R.layout.sdk_dialog_progress, null);
        mProgressText = (TextView) mDialogView.findViewById(R.id.progress_txt);
        //这个layout_base就是我们Activity中内容要显示的一个跟布局
        mFragmentView = (ViewGroup) act.getWindow().findViewById(
                R.id.layout_base);
        mCancelable = cancelable;
        mFLayout = new LinearLayout(act);
        if (!TextUtils.isEmpty(loading_text)) {
            mProgressText.setText(loading_text);
        }
        init();
    }

    /**
     * 
     * 〈一句话功能简述〉<br>
     * 〈功能详细描述〉
     * 
     * @param act content
     * @param loading_text progress显示的String
     */
    public void dialogProgressView(Activity act, String loading_text,
            boolean cancelable) {
        mDialogView = act.getLayoutInflater().inflate(
                R.layout.sdk_dialog_progress, null);
        mProgressText = (TextView) mDialogView.findViewById(R.id.progress_txt);
        mFragmentView = (ViewGroup) act.getWindow().findViewById(
                R.id.skd2_base_content);
        mCancelable = cancelable;
        mFLayout = new LinearLayout(act);
        if (!TextUtils.isEmpty(loading_text)) {
            mProgressText.setText(loading_text);
        }
        init();
    }

    private void init() {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mFLayout.setLayoutParams(params);
        mFLayout.setBackgroundColor(ResUtil.getColor(R.color.sdk_transparent));

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.setMargins(20, 0, 20, 0);

        mFLayout.addView(mDialogView, layoutParams);
        addDialogContentView();

    }

    private void addDialogContentView() {

        if (mFragmentView != null) {
            mFragmentView.addView(mFLayout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
        }
        mFLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 当progress弹出时拦截页面点击事件

            }
        });
    }

    public void dismissProgress() {
        if (mFragmentView != null && mFLayout != null) {
            mFragmentView.removeView(mFLayout);
        }
        reset();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (!mCancelable) {
            reset();
            return true;
        }

        if (keyCode == KeyEvent.KEYCODE_BACK) { // 监控/拦截/屏蔽返回键

            if (mFragmentView == null || mDialogView == null
                    || mFLayout == null) {
                return true;
            }
            dismissProgress();
            return false;
        }

        return true;
    }

    private void reset() {
        mDialogView = null;
        mFragmentView = null;
        mFLayout = null;
    }

}

3:用法

  

//展示
ProgressView.getInstance().showProgressView(avtivityA,"信息加中");

//取消
ProgressView.getInstance().dismissProgress();

抱歉!评论已关闭.