how to show progress bar(circle) in an activity having a listview before loading the listview with data

后端 未结 12 2305
忘掉有多难
忘掉有多难 2020-11-28 00:18

I have a ListView in my second activity.OnItemClick of it I called a webservice and trying to fetch data. And after that I am moving to third activ

相关标签:
12条回答
  • 2020-11-28 00:54

    Use This Within button on Click option or your needs:

    final ProgressDialog progressDialog;
    progressDialog = new ProgressDialog(getApplicationContext());
    progressDialog.setMessage("Loading..."); // Setting Message
    progressDialog.setTitle("ProgressDialog"); // Setting Title
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Progress Dialog Style Spinner
    progressDialog.show(); // Display Progress Dialog
    progressDialog.setCancelable(false);
    new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            progressDialog.dismiss();
        }
    }).start();
    
    0 讨论(0)
  • 2020-11-28 01:00

    I suggest you when working with listview or recyclerview to use SwipeRefreshLayout. Like this

        <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/card_recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    
    </android.support.v4.widget.SwipeRefreshLayout>
    

    Only wrap your view and this will create an animation of refresh when loading data or by swipping down the screen as we can do in many apps.
    Here's the documentation of how to use it:
    https://developer.android.com/training/swipe/add-swipe-interface.html https://developer.android.com/training/swipe/respond-refresh-request.html

    Happy coding !

    0 讨论(0)
  • 2020-11-28 01:02

    Please use the sample at tutorialspoint.com. The whole implementation only needs a few lines of code without changing your xml file. Hope this helps.

    STEP 1: Import library

    import android.app.ProgressDialog;
    

    STEP 2: Declare ProgressDialog global variable

    ProgressDialog loading = null;
    

    STEP 3: Start new ProgressDialog and use the following properties (please be informed that this sample only covers the basic circle loading bar without the real time progress status).

    loading = new ProgressDialog(v.getContext());
    loading.setCancelable(true);
    loading.setMessage(Constant.Message.AuthenticatingUser);
    loading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    

    STEP 4: If you are using AsyncTasks, you can start showing the dialog in onPreExecute method. Otherwise, just place the code in the beginning of your button onClick event.

    loading.show();
    

    STEP 5: If you are using AsyncTasks, you can close the progress dialog by placing the code in onPostExecute method. Otherwise, just place the code before closing your button onClick event.

    loading.dismiss();
    

    Tested it with my Nexus 5 android v4.0.3. Good luck!

    0 讨论(0)
  • 2020-11-28 01:06

    You can add the ProgressBar to listview footer:

    private ListView listview;
    private ProgressBar progressBar;
    

    ...

    progressBar = (ProgressBar) LayoutInflater.from(this).inflate(R.layout.progress_bar, null);
    
    listview.addFooterView(progressBar);
    

    layout/progress_bar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/load_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
         />
    

    when the data is loaded to the adapter view call:

            listview.removeFooterView(progressBar);
    
    0 讨论(0)
  • 2020-11-28 01:07

    I am using this:

    loading = ProgressDialog.show(example.this,"",null, true, true);
    
    0 讨论(0)
  • 2020-11-28 01:08

    Process Bar:

    Dependency:

         implementation 'com.github.castorflex.smoothprogressbar:library:1.0.0'
    

    XML:

         <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
            xmlns:android="http://schemas.android.com/apk/res/android"                
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/myProcessbar"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:indeterminate="true" />
    

    In Res->color

          <color name="pocket_color_1">#ff1635</color>
          <integer-array name="pocket_background_colors">
              <item>@color/pocket_color_1</item>
          </integer-array>
    
          <color name="pocket_color_stop">#00ff00</color>
          <integer-array name="pocket_background_stop">
              <item>@color/pocket_color_stop</item>
          </integer-array>
    

    In Main:

          public class MainActivity extends AppCompatActivity{
            SmoothProgressBar smoothProgressBar;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              smoothProgressBar=findViewById(R.id.myProcessbar);
              showProcessBar();
              stopAnimation();    // call when required to stop process
            }
    
            public void showProcessBar(){
              smoothProgressBar.setVisibility(View.VISIBLE);
              smoothProgressBar.setIndeterminateDrawable(new SmoothProgressDrawable.Builder(getApplicationContext())
                .interpolator(new AccelerateInterpolator())
                .progressiveStart(true)
                .progressiveStopSpeed(1000)
                .build());
              smoothProgressBar.setSmoothProgressDrawableBackgroundDrawable(
              SmoothProgressBarUtils.generateDrawableWithColors(
                        getResources().getIntArray(R.array.pocket_background_colors),
                        ((SmoothProgressDrawable)  smoothProgressBar.getIndeterminateDrawable()).getStrokeWidth()));
            }
    
            public void stopAnimation(){
              smoothProgressBar.setSmoothProgressDrawableBackgroundDrawable(
              SmoothProgressBarUtils.generateDrawableWithColors(
                        getResources().getIntArray(R.array.pocket_background_stop),
                        ((SmoothProgressDrawable)  smoothProgressBar.getIndeterminateDrawable()).getStrokeWidth()));
              smoothProgressBar.progressiveStop();
              Handler handler=new Handler();
              handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                   smoothProgressBar.animate().alpha(0.0f).setDuration(6000).translationY(1000);
                   smoothProgressBar.setVisibility(View.GONE);
                }
              },1000);
    }
    
          }
    
    0 讨论(0)
提交回复
热议问题