I am trying to make a gallery using GridView + Picasso\Universal Image Loader and I faced with some problems. Every time I load only local files (from external storage), not from the web. I make two videos, that describe the problems much better, then words.
I use my custom adapter extends BaseAdapter
.
thumbs
is an array of File
with images
Loads images quickly enough, but very slow scrolling.Here the video demonstration on youtube
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Get the view
View view;
if (convertView == null)
view=inflater.inflate(R.layout.gallery_photo_item,parent,false);
else
view = convertView;
//find the componetnts
final ImageView image_view = (ImageView)view.findViewById(R.id.galley_photo_item_image);
final CheckBox check = (CheckBox)view.findViewById(R.id.galley_photo_item_checkbox);
//Toggling checkbox if DELETE MODE
if(is_checking) {
check.setChecked(false);
check.setVisibility(View.VISIBLE);
}
else
check.setVisibility(View.INVISIBLE);
view.setTag(position);
check.setTag(position);
//Some event handling
check.setOnClickListener(this);
view.setOnClickListener(this);
//Setting up Picasso
Picasso.Builder builder = new Picasso.Builder(context);
builder.listener(new com.squareup.picasso.Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Log.e("Picasso","Loading failed: "+exception.getMessage());
image_view.setVisibility(View.GONE);
}
});
//Display image
Picasso pic = builder.build();
pic
.load(thumbs[position])
//.placeholder(R.drawable.ic_launcher)
.fit()
.into(image_view);
return view;
}
Scrolling quickly, but loading is slow. Also when you scroll fast, you can see the wrong images, which will replace by correct (when they've loaded) for a few seconds. Demonstration on youtube
I added the View Holder
pattern here.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view_holder;
if(convertView==null)
{
convertView = inflater.inflate(R.layout.gallery_photo_item,parent,false);
ImageView image = (ImageView)convertView.findViewById(R.id.galley_photo_item_image);
CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.galley_photo_item_checkbox);
view_holder = new ViewHolder(convertView,image,checkBox);
convertView.setTag(view_holder);
}
else
view_holder = (ViewHolder)convertView.getTag();
//Toggle checkbox on DELETE MODE
if(is_checking) {
view_holder.CheckBox.setChecked(false);
view_holder.CheckBox.setVisibility(View.VISIBLE);
}
else
view_holder.CheckBox.setVisibility(View.INVISIBLE);
//view.setTag(position);
//check.setTag(position);
//Event handling
view_holder.CheckBox.setOnClickListener(this);
view_holder.View.setOnClickListener(this);
//Display image
String uri = Uri.fromFile(thumbs[position]).toString();
ImageLoader loader = ImageLoader.getInstance();
loader.displayImage(uri,view_holder.ImageView, options);
return convertView;
}
What is good way to make gallery working faster (as a native android app, perfectly)?
try using Glide Image Loader Library its recommended by google Refer here:- https://coderzpassion.com/android-working-glide-image-loader-library/