每个Android活动中常见的进度条

5
我已经修改了解决方案。我能够获得进度条,但是进度条从未隐藏。
以下是创建相对布局进度条的类:
public class ProgressBarHandler {
    private ProgressBar mProgressBar;
    private Context mContext;
    private View _baseView;
    private View _hideView;
    public ProgressBarHandler(Context context,View baseView, View hideView) {
        mContext = context;
        _baseView = baseView;
        _hideView = hideView;

        ViewGroup layout = (ViewGroup) _baseView;//((Activity) context).findViewById(android.R.id.content).getRootView();

        mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
        mProgressBar.setIndeterminate(true);

        RelativeLayout.LayoutParams params = new
                RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout rl = new RelativeLayout(context);

        rl.setGravity(Gravity.CENTER);
        rl.addView(mProgressBar);

        layout.addView(rl, params);

        hide();
    }

    public void show() {
        mProgressBar.setVisibility(View.VISIBLE);
        _hideView.setVisibility(View.INVISIBLE);
    }

    public void hide() {
        mProgressBar.setVisibility(View.INVISIBLE);
        _hideView.setVisibility(View.VISIBLE);
    }
}

这是我的XML文件。
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:id="@+id/profile_activity">


<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
    android:id="@+id/layout_to_hide"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:expandedTitleTextAppearance="@android:color/transparent"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@mipmap/wallpaper"
                android:orientation="vertical">

                <com.deerwalk.androidcommon.RoundedImageView
                    android:id="@+id/profile_image"
                    android:layout_width="100dp"
                    app:civ_border_color="#EEEEEE"
                    app:civ_border_width="4dp"
                    app:civ_shadow="true"
                    app:civ_shadow_radius="10"
                    app:civ_shadow_color="#8BC34A"
                    android:layout_height="100dp"
                    android:layout_alignWithParentIfMissing="false"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:layout_margin="20dp"
                    android:src="@drawable/avatar" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_below="@+id/profile_image"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:id="@+id/txt_name"
                        android:layout_gravity="center_horizontal"
                        android:text=""
                        android:textColor="@color/white" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:id="@+id/txt_dob"
                        android:layout_gravity="center_horizontal"
                        android:text=""
                        android:layout_marginTop="5dp"
                        android:textColor="@color/white" />
                </LinearLayout>
            </RelativeLayout>



        </LinearLayout>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


</android.support.v7.widget.RecyclerView>

</android.support.design.widget.CoordinatorLayout>
</FrameLayout>

这是调用进度条的活动类。
public class ProfileActivity extends AppCompatActivity {

    FrameLayout profile_root_layout;
    CoordinatorLayout layot_to_hide;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        profile_root_layout = (FrameLayout) findViewById(R.id.profile_activity);
        layot_to_hide = (CoordinatorLayout) findViewById(R.id.layout_to_hide);
         new userProfileTask().execute();
    }




    public class userProfileTask extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected void onPreExecute() {
            new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).show();
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                Thread.sleep(2000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {

            new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).hide();
        }
    }

}

进度条始终没有隐藏。我做错了什么?

每个活动都有自己的布局,因此您不能为所有活动使用通用进度条。 - vabhi vab
是的,我知道每个活动都有自己的布局。我认为我们可以使用布局膨胀。 - Ananda Prasad Jaisy
为什么不要在每个需要显示进度对话框的活动中创建一个对话框并进行调用。 - Nitesh
5个回答

1
我建议创建一个对话框并在您的活动类中调用,如下所示。
dialog = new Dialog(getActivity(), android.R.style.Theme_Black);
    View views = LayoutInflater.from(getActivity()).inflate(R.layout.activity_dialog, null);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawableResource(R.color.feed_item_bg);
    dialog.setContentView(views);
    dialog.show();

在您的异步任务 onPostExecute 中取消对话框。
dialog.dismiss();

1
创建一个独立的类,然后在其中创建一个带有参数的静态方法,该参数包含Activity... 在您的活动上调用此方法,并将Activity名称传递给它。

请问您能否展示一个示例?这对我来说非常有帮助...我已经尝试了一天。 - Ananda Prasad Jaisy
公共类 Config {public static void setProgressBar(Activity activity){ progress=new ProgressDialog(activity); progress.setMessage("下载音乐中"); progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progress.setIndeterminate(true); progress.setProgress(0); progress.show(); } - Amit Ranjan
在您的活动类中,您只需调用 Config.setProgressBar(YourActivityName)。 - Amit Ranjan
我该如何通过进度条来实现这个? - Ananda Prasad Jaisy
以上是我创建的独立 Config 类,在其中我创建了静态方法,并在您的活动类中调用此方法。 - Amit Ranjan
它运行良好,谢谢,但我需要带有进度条的版本。你能展示一下带有进度条的吗? - Ananda Prasad Jaisy

1

非常简单,你可以用多种方式实现。 1)你可以定义一个基本活动(BaseActivity),在其中编写进度对话框的代码,并将所有活动(Activity)从BaseActivity继承,并使用Progressdialog。 2)你可以有一个自定义的独立类,并在所有活动中使用它。 等等

这是一个好的例子,你可以像这个例子一样修改你的代码。 ProgressHud

或者也可以使用这个。


我需要关于进度条的帮助。 - Ananda Prasad Jaisy
是的,您也可以制作进度条,它只是提供了显示常见进度条的想法,您可以根据需要修改progressHud以满足您的要求。 - khurram

0
public static void getProgress(Activity activity,FrameLayout relativeLayout){
final ProgressBar pb;
int progressStatus = 0;
final Handler handler = new Handler();
pb = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
LayoutParams lp = new LayoutParams(550,LayoutParams.WRAP_CONTENT);
pb.setLayoutParams(lp);
LayoutParams params = (LayoutParams) pb.getLayoutParams();
pb.setLayoutParams(params);
pb.getProgressDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN);
relativeLayout.addView(pb);
new Thread(new Runnable() {
@Override
public void run() {
int progressStatus = 0;
while(progressStatus < 100){
progressStatus +=1;
try{
Thread.sleep(20);
}catch(InterruptedException e){
e.printStackTrace();
}
final int finalProgressStatus = progressStatus;
handler.post(new Runnable() {
@Override
public void run() {
pb.setProgress(finalProgressStatus);
}
});
}
}
}).start(); // Start the operation
}

将此代码添加到您的配置类中并告诉我。 - Amit Ranjan

0
你可以使用一个父Activity。这个Activity类继承自AppCompatActivity。其他的Activities将会继承这个Activity。例如,这个Activity的名字是AbstractActivty。
public class  AbstractActivty extends AppCompatActivity{

      AlertDialog alertDialogProgressBar;

      public void showLoadingProgressBar(String message, boolean cancalable) {
        if (isActivityPaused)
            return;

          AlertDialog.Builder adb = new AlertDialog.Builder(this);
          View view = getLayoutInflater().inflate(R.layout.alert_dialog_progressbar_layout,null);
          TextView tv = (TextView) view.findViewById(R.id.idTextViewMessage);
          tv.setText(message);
          adb.setView(view);
          alertDialogProgressBar = adb.create();
          alertDialogProgressBar.setCancelable(cancalable);
          alertDialogProgressBar.show();
}

 public void closeProgressBar() {
        if (alertDialogProgressBar == null) {
            return;
        }
        alertDialogProgressBar.hide();
    }

}

这个类有一个方法,可以被任何继承AbstractActivity的其他活动调用。

class MainActivty extends AbstractActivty{
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // when require to show progressbar
        showLoadingProgressBar("Loading....",true)

        // when required to hide call
        closeProgressBar();

 }

}    

注意,在 Alert 对话框中,我们使用了自定义布局“alert_dialog_progressbar_layout.xml”

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/text_padding_l">


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineVertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".3"
        />


    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guidelineVertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/idTextViewMessage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Loading...."
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/guidelineVertical"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

希望这能有所帮助。谢谢。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接