Reputation:
I wonder if I'm doing anything wrong. My code is pretty straight forward and would like an example of how you guys made it work if you have a progress bar to load an image in any of your apps, thanks! I'm using volley's imageloader and the progress bar never ever appears
holder.mProgressBar.setVisibility(View.VISIBLE);
if (video.thumbnail != null) {
mImageLoader.get(video.thumbnail, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.mProgressBar.setVisibility(View.GONE);
holder.mScreenShot.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
holder.mProgressBar.setVisibility(View.GONE);
}
});
}
My XML
<RelativeLayout
android:id="@+id/image_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:background="@android:color/darker_gray"
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="@dimen/album_cover_height"
android:clickable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:scaleType="fitXY" />
<ProgressBar
android:visibility="gone"
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
Upvotes: 1
Views: 418
Reputation: 306
Definitely a timing issue. I replicated your scenario as best as I could.
public class MainActivity extends AppCompatActivity {
ImageView image;
ProgressBar pb;
public String url;
public ImageLoader imageLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
pb = (ProgressBar) findViewById(R.id.pb);
RequestQueue queue = Volley.newRequestQueue(this);
url = "http://www.hrwiki.org/w/images/thumb/d/d5/currentbad.png/180px-currentbad.png";
imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
show();
new testTask().execute(imageLoader);
}
public void show() {
image.setVisibility(View.INVISIBLE);
pb.setVisibility(View.VISIBLE);
}
public void hide() {
image.setVisibility(View.VISIBLE);
pb.setVisibility(View.INVISIBLE);
}
private class testTask extends AsyncTask<ImageLoader, Void, Void> {
@Override
protected Void doInBackground(ImageLoader... params) {
SystemClock.sleep(5000);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
imageLoader.get(url, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
hide();
image.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
}
With this layout file:
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="@+id/image" />
<ProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:id="@+id/pb"/>
</FrameLayout>
I saw the same behavior until I put the aSync task in which basically waits for 5 seconds before loading the image. This definitely shows that OnResponse is getting called so quickly that you aren't seeing the progress bar.
Looks like Michelle's answer has what you would look for to implement some pre-check logic.
Upvotes: 1
Reputation: 61
Refer to this previous question for a detailed explanation for how onResponse(ImageLoader.ImageContainer response, boolean isImmediate)
is working.
Volley, How many times the onResponse in ImageLoader.ImageListener called
I believe it is basically just immediately turning off your progress bar. Haven't done this particular thing before but based on the other question I think you want to add a check for:
if(!isImmediate)
holder.mProgressBar.setVisibility(View.GONE);
Upvotes: 0