我正在开发一款社交媒体应用。基本上这是一个照片共享应用程序。我正在 GridView 中加载图片,除image loading speed
外一切正常。速度降低,因为它同时加载所有图像。如何才能首先加载少量图像然后它会在scroll
上加载更多?
我想要的更多可能是第一次获得10个图像,等等每次滚动后获得更多图像,直到所有图像都被加载。
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;
}
}
答案 0 :(得分:0)
即使我已经从这个问题中受了很多苦,但这是一个真正的Goode解决方案。
您可以使用此库名为Glide。
这是您必须使用的方式
将此添加到您的Gradel文件
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
}
这就是加载和设置图像到GridView的方式
GlideApp
.with(myFragment)
.load(path/url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.into(myImageView);
这是一个非常优秀的图书馆。希望它可以帮到你。