57773

how to set progress bar when recycler view is clicked

Question:

I have an app which gets data from json and displays it on recycler view and when each recycler view is clicked it opens a new activity to show full content. all i want to know is how to show a progress dialog befrore the second activity shows thanks. Here is my code

public CustomViewHolder(View view, Context ctx, ArrayList<FeedItem> feeditem) { super(view); view.setOnClickListener(this); this.ctx = ctx; this.feeditem = feeditem; this.imageView = (ImageView) view.findViewById(R.id.thumbnail); this.textView2 = (TextView) view.findViewById(R.id.date); this.textView3 = (TextView) view.findViewById(R.id.excerpt); this.categories = (TextView) view.findViewById(R.id.categories); this.textView = (TextView) view.findViewById(R.id.title); } @Override public void onClick(View v) { int position = getAdapterPosition(); FeedItem feeditem = this.feeditem.get(position); Intent intent = new Intent(this.ctx, ScrollingActivity.class); intent.putExtra("excerpt",feeditem.getExcerpt()); intent.putExtra("content",feeditem.getContent()); intent.putExtra("title",feeditem.getTitle()); Html.fromHtml(String.valueOf(intent.putExtra("content",feeditem.getContent()))).toString(); intent.putExtra("thumbnail",feeditem.getAttachmentUrl()); this.ctx.startActivity(intent); }

Answer1:

You have to deal with it in your second activity. There, you should do all your data loading or heavy processing work in a background thread and update the UI as and when you have the data or processing is completed.

<strong>AsyncTask</strong> is designed to serve exactly this purpose

public class LoadDataTask extends AsyncTask<Void, Void, Void> { public LoadDataTask(ProgressDialog progress) { this.progress = progress; } public void onPreExecute() { progress.show(); } public void doInBackground(Void... unused) { ... load your image here ... } public void onPostExecute(Void unused) { progress.dismiss(); } }

onPreExecute() and onPostExecute() run on the UI thread. So you can update your UI here, like showing the image.

Your second activity should look like this now:

ProgressDialog progress = new ProgressDialog(this); progress.setMessage("Loading..."); new LoadDataTask(progress).execute();

For further help, check these:

<ul><li>

<a href="https://stackoverflow.com/a/5202186/3857465" rel="nofollow">How to display progress dialog before starting an activity in Android?</a>

</li> <li>

<a href="https://www.google.co.in/url?sa=t&source=web&rct=j&url=https://developer.android.com/reference/android/os/AsyncTask.html&ved=0ahUKEwjn7a3y6NnRAhVLtI8KHYjBBbAQFggZMAA&usg=AFQjCNGLaSuxTknRpqLNZwIIM0qNhRQDwA&sig2=V6RebYOiR0adN6pFzI7Grg" rel="nofollow">AsyncTask | Android Developers</a>

</li> </ul>

Recommend

  • SetPixel() too slow to erase image background
  • Using std::complex with iPhone's vDSP functions
  • python — measuring pixel brightness
  • What should I do to not show the legend for all the plots?
  • Unexpected behavior of numpy.fft.fft with high precision numbers
  • FFT wrong value?
  • Creating Fragments for login
  • Casting an array of C structs to a numpy array
  • Can't set visibility on constraint group
  • convert full scrollable textview to bitmap
  • Mayavi: interpolate face colors in triangular_mesh
  • using viewHolder Interface to update state to Listview
  • How to configure a non-default serviceAccount on a deployment
  • WP8 Memory leak opening and closing PhoneApplicationPage
  • Upload an embed image?
  • CarrierWave: Single uploader for multiple models?
  • Fancy dynamic list in Android: TableLayout vs ListView
  • How to get a android ListView item selector to use state_pressed
  • Why does .addView throw this parent/child exception?
  • How to Handle Click on Imageview in Custom Listview with image and Textview?
  • Android: How do you create an EditText field in java class
  • Picasso Taking time to load images
  • Null ImageView Reference
  • CloseOptionsMenu doesn't work?
  • Unable to resolve Static method?
  • How to set my toolbar fixed while scrolling android
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Android Heatmap on canvas or ImageView
  • Conditional In-Line CSS for IE and Others?