I am developing an social media app. Basically this is a photo sharing app. I am loading images in GridView and everything working fine except image loading speed
. Speed is decreased because it is loading all images at same time. How can i load only few images first then it will load more on scroll
?.
What I want More likely is getting e.g, 10 images at first time and so on after scrolling each time and get more images untill all are loaded.
MyGridAdapter
public class MyGridAdapter extends BaseAdapter {
private Context context;
private ArrayList<ImageObject> imageObjects;
private LayoutInflater mLayoutInflate;
public MyGridAdapter(Context context, ArrayList<ImageObject> imageObjects) {
this.context = context;
this.imageObjects = imageObjects;
this.mLayoutInflate = LayoutInflater.from(context);
}
public int getCount() {
if (imageObjects != null) return imageObjects.size();
return 0;
}
@Override
public Object getItem(int position) {
if (imageObjects != null && imageObjects.size() > position)
return imageObjects.get(position);
return null;
}
@Override
public long getItemId(int position) {
if (imageObjects != null && imageObjects.size() > position)
return imageObjects.get(position).getId();
return 0;
}
public String getImageId(int position){
if (imageObjects != null && imageObjects.size() > position)
return imageObjects.get(position).getImageid();
return null;
}
public String getUsername(int position){
if (imageObjects != null && imageObjects.size() > position)
return imageObjects.get(position).getUsername();
return null;
}
public String getCaption(int position){
if (imageObjects != null && imageObjects.size() > position)
return imageObjects.get(position).getCaption();
return null;
}
public String getProfilePic(int position){
if (imageObjects != null && imageObjects.size() > position)
return imageObjects.get(position).getProfilepic();
return null;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mLayoutInflate.inflate(R.layout.imageitem, parent,
false);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ImageObject imageObject = (ImageObject) getItem(position);
if (imageObject != null) {
Glide
.with(context)
.load(imageObject.getImageUrl())
.priority(Priority.HIGH)
.placeholder(R.drawable.image_load_anim)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.crossFade()
.centerCrop()
.into(viewHolder.imageView);
}
return convertView;
}
}