Skip to content

Instantly share code, notes, and snippets.

@lokcito
Last active December 19, 2019 02:17
Show Gist options
  • Save lokcito/dc7c938d2f7025f2a40e7349e970cccb to your computer and use it in GitHub Desktop.
Save lokcito/dc7c938d2f7025f2a40e7349e970cccb to your computer and use it in GitHub Desktop.
ListView con imagenes
// Rutas:
// https://mymodernmet.com/wp/wp-content/uploads/2019/09/100k-ai-faces-3.jpg
// https://mymodernmet.com/wp/wp-content/uploads/2019/09/100k-ai-faces-8.jpg
// https://mymodernmet.com/wp/wp-content/uploads/2019/09/100k-ai-faces-7.jpg
// https://mymodernmet.com/wp/wp-content/uploads/2019/09/100k-ai-faces-4.jpg
// build.gradle (Module: App)
dependencies {
....
implementation 'com.android.volley:volley:1.1.1'
}
//====================================================
// helpers/QueueUtils.java
package info.rayrojas.bichito.frutapp.helpers;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class QueueUtils {
private static QueueObject uniqueInstance;
public static class QueueObject {
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private QueueObject(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
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);
}
});
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
public static synchronized QueueObject getInstance(Context context) {
if (uniqueInstance == null) {
uniqueInstance = new QueueObject(context);
}
return uniqueInstance;
}
}
//====================================================
// En ActivityMain.java
public class ProductListActivity extends AppCompatActivity {
...
QueueObject queue = null;
...
ProductAdapter itemsAdapter;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
queue = QueueUtils.getInstance(this.getApplicationContext());
itemsAdapter =
new ProductAdapter(this, items, queue.getImageLoader());
...
}
//====================================================
// Layout
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp" />
//====================================================
// ProductAdapter.java
...
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
...
public class ProductAdapter extends ArrayAdapter<Product> {
Context context;
ImageLoader queue; //<-------- Añadir esta variable
private class ViewHolder {
NetworkImageView image; //<-------- Añadir esta variable
TextView name;
TextView price;
...
private ViewHolder() {
}
}
...
public ProductAdapter(Context context, List<Product> items, ImageLoader _queue) { //<-------- Verificar ImageLoader
super(context, 0, items);
this.context = context;
this.queue = _queue; //<---- Notar esa linea
}
public View getView(final int position, View convertView, ViewGroup parent) {
...
holder.image = (NetworkImageView)convertView.findViewById(R.id.image); //<----- Notar esta linea
...
if ( rowItem.getSmallImage() != null ) { //<----- El modelo debe poseer esta function
holder.image.setImageUrl(rowItem.getSmallImage(), queue);
}
...
return convertView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment